【问题标题】:UIBarButtonItem segue to next viewcontrollerUIBarButtonItem 转到下一个视图控制器
【发布时间】:2014-12-11 06:58:27
【问题描述】:

我以编程方式创建了一个 UIBarButtonItem,并希望它在被推送时转到下一个 ViewController。

这就是我在 viewDidLoad 中创建自定义 UIBarButtonItem 的方式:

  UIBarButtonItem *foundButton = [[UIBarButtonItem alloc]
                               initWithTitle:@"Found it!"
                               style:UIBarButtonItemStyleBordered
                               target:self
                               action:@selector(foundView)];

之后我创建了这个方法:

-(void)foundView:(id)sender {

 UIViewController *foundVC = [self.storyboard instantiateViewControllerWithIdentifier:@"foundView"];

 [self.navigationController pushViewController:foundVC animated:YES];
}

点击 UIBarButtonItem 后我想去的 ViewController 有一个 Storyboard ID:“foundView”。

我在这里做错了什么?

【问题讨论】:

    标签: ios iphone objective-c uiviewcontroller


    【解决方案1】:

    像这样更改您的代码:

    UIBarButtonItem *foundButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Found it!"
                                   style:UIBarButtonItemStyleBordered
                                   target:self
                                   action:@selector(foundView:)];
    

    您在选择器中的foundView 之后忘记了:,因此您不会调用该方法。

    【讨论】:

    • 也看看@nhgrif 的回答
    【解决方案2】:

    您可以创建一个从ViewControllerAViewControllerBsegue。注意,这个segue 连接了两个视图控制器。不是视图控制器的按钮。现在,在故事板上,给 segue 一个描述性的名称。例如:segueFoundView

    现在,这样做:

    -(void)foundView:(id)sender {
        [self performSegueWithIdentifier:@"segueFoundView" sender:self];
    }
    

    编辑:更正拼写

    【讨论】:

    • 如果有人在进行复制/粘贴,应该是performSegueWithIdentifier
    猜你喜欢
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多