2012-12-13 43 views
0

我正在用自己的图像创建一个自定义按钮,并将它分配给我拥有的nabvigationcontroller的右侧按钮。问题是,当用户点击按钮时,我得到一个关于我的控制器的exceptoin而不能识别该选择器?为UIBarButton创建自定义按钮,选择器抛出异常

UIImage* image1 = [UIImage imageNamed:@"someImage.png"]; 
CGRect imgRect = CGRectMake(0, 0, image1.size.width/1.8, image1.size.height/1.8); 
UIButton *myButton = [[UIButton alloc] initWithFrame:imgRect]; 
[myButton setBackgroundImage:image1 forState:UIControlStateNormal]; 
[myButton addTarget:self action:@selector(nextScreenButtonAction) forControlEvents:UIControlEventTouchUpInside]; 


UIBarButtonItem *barButton =[[UIBarButtonItem alloc] initWithCustomView:myButton]; 
[self.navigationItem setRightBarButtonItem:myButton]; 
self.navigationController.navigationBarHidden = false; 
+2

可能是这个问题'nextScreenButtonAction'改变它'[ myButton addTarget:self action:@selector(nextScreenButtonAction :) forControlEvents:UIControlEventTouchUpInside]' –

+0

这样做了,哇,现在请解释为什么:在最后添加了差异? – Kalamantina

+0

在你的方法定义中,你需要传递和参数,如' - (IBAction)nextScreenButtonAction:(id)sender {}'所以在addTarget中,你需要指定一个冒号(:)来表示参数 –

回答

1

可能这就是问题所在nextScreenButtonAction将其更改为

[myButton addTarget:self action:@selector(nextScreenButtonAction:) forControlEvents:UIControlEventTouchUpInside] 

在你的方法定义你路过和论证像

-(IBAction) nextScreenButtonAction:(id)sender 
{ 
} 

所以在addTarget你需要指定一个冒号(:)指示参数

0

在回答下面的代码,

 
[myButton addTarget:self action:@selector(nextScreenButtonAction) forControlEvents:UIControlEventTouchUpInside]; 

请确保您有在同一类如创建方法,

 
- (void) nextScreenButtonAction 
{ 
    // write your logic here... 
}