UIBarButton没有执行Action

时间:2013-09-01 10:22:19

标签: ios uinavigationcontroller uibarbuttonitem

我遇到了问题:我在我的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

2 个答案:

答案 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。否则,没有人接受这个行动!