自定义栏按钮项不执行segue

时间:2017-02-18 07:00:42

标签: ios objective-c segue

我在viewDidLoad中定义了一个正确的条形按钮项目:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UIButton *helpButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:helpButton ];

}

我已经链接了条形按钮项目并使用标识符执行了segue但它没有推动视图。为什么会这样?

- (IBAction)btnShowHelp:(id)sender {

    [self performSegueWithIdentifier:@"showHelp" sender:self];
}

2 个答案:

答案 0 :(得分:1)

删除按钮与- (IBAction)btnShowHelp:(id)sender之间的链接,将方法更改为- (void)btnShowHelp(id)sender,然后更改viewDidLoad中的代码,如下所示:

    UIButton *helpButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
    [helpButton addTarget:self action:@selector(btnShowHelp:) forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:helpButton ];

答案 1 :(得分:0)

事实证明,我的故事板中的btnShowHelp已经被viewDidLoad中定义的新UIButton取代,并且不需要它。

https://developer.apple.com/reference/uikit/uibarbuttonitem/1617151-initwithcustomview?language=objc

  

此方法创建的条形按钮项不会调用其目标的操作方法以响应用户交互。相反,bar按钮项期望指定的自定义视图处理任何用户交互并提供适当的响应。

我正在做[self.navigationItem.rightBarButtonItem setAction:]然后我意识到我想要为我的UIButton设置动作而不是条形按钮项 ......

UIButton *helpButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[helpButton addTarget:self action:@selector(showHelp) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:helpButton ];

- (void)showHelp {

    [self performSegueWithIdentifier:@"showHelp" sender:self];
}

Adding action and target to a custom Navigation BarButtonItem?