【问题标题】:Custom bar button item not performing segue自定义栏按钮项目不执行 segue
【发布时间】:2017-02-18 15:20:47
【问题描述】:

我有一个在 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];
}

【问题讨论】:

    标签: ios objective-c segue


    【解决方案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 ];
    

    【讨论】:

    • 提个建议:尽量不要用XIB,用代码实现你想要的效果。
    • 谢谢!和你同时想通了哈哈
    • 您可以建议使用addTarget .. 方法添加目标仅在按钮和实际视图之间建立链接。
    【解决方案2】:

    原来我故事板中的 btnShowHelp 已经被 viewDidLoad 中定义的新 UIButton 替换了,因此不需要它。

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

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

    我正在做[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?

    【讨论】:

      猜你喜欢
      • 2015-01-08
      • 2020-08-18
      • 1970-01-01
      • 2012-04-10
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      相关资源
      最近更新 更多