【问题标题】:Programmatically add IBAction to UIButton以编程方式将 IBAction 添加到 UIButton
【发布时间】:2011-06-30 12:35:14
【问题描述】:

我正在尝试向 UIButton 添加操作,但不断收到异常:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“[UIImageView addTarget:action:forControlEvents:]: 无法识别的选择器发送到实例 0x595fba0”

这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *myIcon = [UIImage imageNamed:@"Icon_Profile"];
    self.profileButton = (UIButton*)[[UIImageView alloc] initWithImage:myIcon];

    [self.profileButton addTarget:self action:@selector(profileButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *buttonItem = [[[UIBarButtonItem alloc] initWithCustomView:profileButton] autorelease];

    NSArray *toolbarItems = [[NSArray alloc] initWithObjects:buttonItem, nil];

    [self setToolbarItems:toolbarItems animated:NO];

    //[toolbarItems release];
    //[profileButton release];
}

然后我在同一个视图控制器中有这个方法:

-(void)profileButtonPressed:(id)sender{

}

在标题中我有

-(IBAction)profileButtonPressed:(id)sender;

发生了什么事?

【问题讨论】:

    标签: ios objective-c ibaction


    【解决方案1】:

    您正在将UIImageView 投射到UIButton,它不会响应addTarget:action:forControlEvents:。使用setBackgroundImage:forState:setImage:forState:UIButton 创建实际按钮并为不同状态设置图像。

    【讨论】:

      【解决方案2】:

      为什么要将 UIImageView 投射到按钮中。

      UIImage *myIcon = [UIImage imageNamed:@"Icon_Profile"];
      self.profileButton = [UIButton buttonWithStyle:UIButtonStyleCustom];
      [self.profileButton setImage:myIcon forState:UIControlStateNormal];
      [self.profileButton addTarget:self action:@selector(profileButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
      

      【讨论】:

      • setImage: 或 setImage:forState: ?
      【解决方案3】:

      您不能将UIImageView 对象转换为UIButton 并期望它的行为类似于UIButton。由于您打算创建一个UIBarButtonItem,因此请使用initWithImage:style:target:action: 使用图像对其进行初始化。

      UIBarButtonItem *buttonItem = [[[UIBarButtonItem alloc] initWithImage:myIcon style:UIBarButtonItemStylePlain target:self action:@selector(profileButtonPressed:)] autorelease]; 
      

      我认为这比创建 UIButton 并将其分配为自定义视图更好。

      【讨论】:

        【解决方案4】:

        这看起来很不对:

        self.profileButton = (UIButton*)[[UIImageView alloc] initWithImage:myIcon];
        

        UIImageView 不是UIButton。你应该allocinit一个正确的UIButton,然后你可以调用

        [self.profileButton setImage: myIcon forState:UIControlStateNormal];
        

        【讨论】:

          【解决方案5】:

          首先创建您自己的按钮。并在之后添加操作:

          UIImage *myIcon = [UIImage imageNamed:@"Icon_Profile"];
          UIButton *buttonPlay = [UIButton buttonWithType:UIButtonTypeCustom];
          buttonPlay.frame = CGRectMake(0, 0, 20, 20);
          [buttonPlay setBackgroundImage:myIcon forState:UIControlStateNormal];
          [buttonPlay addTarget:self action:@selector(buttonPlayClick:) forControlEvents:UIControlEventTouchUpInside];
          

          你的选择器应该是这样的

          - (void)buttonPlayClick:(UIButton*)sender{
          }
          

          现在您可以创建自定义栏项

          UIBarButtonItem *buttonItem = [[[UIBarButtonItem alloc] initWithCustomView:buttonPlay] autorelease];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多