以编程方式将 IBAction 添加到 UIButton

发布于 2024-11-18 00:44:17 字数 1054 浏览 3 评论 0原文

我试图向 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;

怎么回事?

I am trying to add an action to a UIButton, but keep getting an exception:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '[UIImageView addTarget:action:forControlEvents:]: unrecognized selector sent to instance 0x595fba0'

Here is my code:

- (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];
}

Then I have this method in the same View controller:

-(void)profileButtonPressed:(id)sender{

}

And in the header I have

-(IBAction)profileButtonPressed:(id)sender;

What's going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

慕烟庭风 2024-11-25 00:44:17

您正在将 UIImageView 转换为 UIButton,但它不响应 addTarget:action:forControlEvents:。使用 UIButtonsetBackgroundImage:forState:setImage:forState: 创建一个实际的按钮并设置不同状态的图像。

You are casting an UIImageView to UIButton which does not respond to addTarget:action:forControlEvents:. Create an actual button and set image for different states by using setBackgroundImage:forState: or setImage:forState: of UIButton.

酒解孤独 2024-11-25 00:44:17

为什么要将 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];

Why are you casting a UIImageView into a button.

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];
看轻我的陪伴 2024-11-25 00:44:17

您无法将 UIImageView 对象强制转换为 UIButton 并期望它的行为类似于 UIButton。由于您打算创建一个 UIBarButtonItem,请使用 initWithImage:style:target:action: 使用图像来初始化它。

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

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

You can't cast a UIImageView object to UIButton and expect it to behave like a UIButton. Since you intend to create a UIBarButtonItem, use initWithImage:style:target:action: to init it with an image.

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

I think this is a better approach over creating a UIButton and assigning it as a custom view.

︶葆Ⅱㄣ 2024-11-25 00:44:17

这看起来非常错误:

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

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

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

This looks very wrong:

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

A UIImageView is not a UIButton. You should alloc and init a proper UIButton, and then you can call

[self.profileButton setImage: myIcon forState:UIControlStateNormal];
若水般的淡然安静女子 2024-11-25 00:44:17

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

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];

Create your own button at first. And add action after:

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];

And your selector should be like this

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

Now you can create custom bar item

UIBarButtonItem *buttonItem = [[[UIBarButtonItem alloc] initWithCustomView:buttonPlay] autorelease];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文