0

我遇到了一个问题:我在我的 UiNavigation 控制器中以编程方式创建了一个 UIBarButton。但是,我希望它在按下时发出警报。但是,当我使用带有自定义文本的自定义栏按钮时,它不起作用:

self.navigationItem.rightBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Subscribe" style:UIBarButtonItemStyleDone target:nil action:@selector(subscribe:)];

self.navigationItem.rightBarButtonItem = addButton;

但是当我做一个普通的条形按钮时,它工作正常:

self.navigationItem.rightBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(subscribe:)];

self.navigationItem.rightBarButtonItem = addButton;

这里是空白:

-(void)subscribe:(id)sender {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Highscore" message:@"" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];

    [alert show];

}

提前致谢, 拉菲

4

2 回答 2

2

替换您在 init 调用中nil传递的内容target:

[[UIBarButtonItem alloc] initWithTitle:@"Subscribe" 
                                 style:UIBarButtonItemStyleDone 
                                target:nil  <---
                                action:@selector(subscribe:)];

self. 您的 alloc init 应该如下所示:

[[UIBarButtonItem alloc] initWithTitle:@"Subscribe" 
                                 style:UIBarButtonItemStyleDone 
                                target:self
                                action:@selector(subscribe:)];

发给 nil 的消息不会产生影响 ;-)

于 2013-09-01T10:24:06.090 回答
0

target一定是self,不是nil。否则没有人收到行动!

于 2013-09-01T10:24:14.200 回答