2012-07-07 109 views
0

我的代码:的NavBar按钮的操作不工作

-(void)loadView { 
[super loadView]; 
self.informationButton = [[UIBarButtonItem alloc] initWithCustomView:[UIButton buttonWithType:UIButtonTypeInfoLight]]; 
} 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
self.navigationItem.leftBarButtonItem = self.informationButton; 
self.informationButton.target = self; 
self.informationButton.action = @selector(showHelpInfo); 
} 

-(void)showHelpInfo { 
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test" 
               message:@"Test" 
               delegate:nil 
             cancelButtonTitle:@"cansel" 
             otherButtonTitles:nil, nil]; 
[alert show]; 
} 

然后我点击我的informationButton UIAlertView中不显示。有我的错误?

回答

2

你的UIButton是现在调度动作和事件的东西,因为UIBarButtonItem只是它的一个容器。所以,当你点击那个按钮时,就是拦截触摸事件的UIButton。只需制作一个与UIBarButtonItem具有相同动作的UIButton对象即可。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UIButton *iButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
    [iButton addTarget:self action:@selector(showHelpInfo) forControlEvents:UIControlEventTouchUpInside]; 
    self.informationButton = [[UIBarButtonItem alloc] initWithCustomView:iButton]; 
    self.navigationItem.leftBarButtonItem = self.informationButton; 
} 
1

您需要设置UIButton的目标是showHelpInfo选择...