我遇到了问题:我在我的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];
}
提前致谢, Rafee
答案 0 :(得分:2)
在初始化调用中将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的消息不起作用; - )
答案 1 :(得分:0)
target
必须是self
,而不是nil
。否则,没有人接受这个行动!