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

informationButton次に、 uialertviewをクリックしても表示されません。私のエラーがありますか?

4

2 に答える 2

2

UIBarButtonItemはこの時点では単なるコンテナであるため、UIButtonはアクションとイベントをディスパッチするものです。したがって、そのボタンをクリックすると、タッチイベントをインターセプトするのはUIButtonです。同じアクションを持つUIButtonオブジェクトをUIBarButtonItemにするだけです。

- (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

セレクターUIButtonになるターゲットを設定する必要があります...showHelpInfo

于 2012-07-07T20:34:18.750 に答える