以编程方式将 IBAction 添加到 UIButton
我试图向 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在将
UIImageView
转换为UIButton
,但它不响应addTarget:action:forControlEvents:
。使用UIButton
的setBackgroundImage:forState:
或setImage:forState:
创建一个实际的按钮并设置不同状态的图像。You are casting an
UIImageView
toUIButton
which does not respond toaddTarget:action:forControlEvents:
. Create an actual button and set image for different states by usingsetBackgroundImage:forState:
orsetImage:forState:
ofUIButton
.为什么要将 UIImageView 投射到按钮中。
Why are you casting a UIImageView into a button.
您无法将
UIImageView
对象强制转换为UIButton
并期望它的行为类似于UIButton
。由于您打算创建一个UIBarButtonItem
,请使用initWithImage:style:target:action:
使用图像来初始化它。我认为这是比创建 UIButton 并将其分配为自定义视图更好的方法。
You can't cast a
UIImageView
object toUIButton
and expect it to behave like aUIButton
. Since you intend to create aUIBarButtonItem
, useinitWithImage:style:target:action:
to init it with an image.I think this is a better approach over creating a
UIButton
and assigning it as a custom view.这看起来非常错误:
UIImageView
不是UIButton
。您应该alloc
和init
一个正确的UIButton
,然后您可以调用This looks very wrong:
A
UIImageView
is not aUIButton
. You shouldalloc
andinit
a properUIButton
, and then you can call首先创建您自己的按钮。并在之后添加操作:
并且您的选择器应该像这样
现在您可以创建自定义栏项
Create your own button at first. And add action after:
And your selector should be like this
Now you can create custom bar item