导航按钮的操作无效

时间:2012-07-07 20:27:52

标签: iphone objective-c ios

我的代码:

-(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 个答案:

答案 0 :(得分: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 :(得分:1)

您需要将UIButton的目标设置为showHelpInfo选择器...