0

我的代码:

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

然后我点击我的informationButtonuialertview 不显示。有我的错误吗?

4

2 回答 2

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;
}
于 2012-07-07T20:33:11.037 回答
1

您需要将目标设置UIButtonshowHelpInfo选择器...

于 2012-07-07T20:34:18.750 回答