iOS 8:如何使用segues

iOS 8: How to use segues

我对转场真的很困惑。有很多不同的方法可以改变当前的 ViewController,我不知道该用哪个,把它们混在一起。

我目前拥有的是 MainViewController。在这上面我有 4 个按钮。第一个按钮与 Show(例如 Push)Segue 连接。在 Segue 的另一边是带有视图控制器的导航控制器。现在无需额外代码即可使用。

现在是我的第一个问题。 Navigation Controller 永远不会显示,对吧?只会显示视图控制器。但是如果我想在右侧添加一个Navigation Item Button 回到第一个View Controller 应该怎么做呢?我需要像这样添加按钮程序:

UIBarButtonItem *leftBackToPreviousScreenButton = [[UIBarButtonItem alloc]
                                              initWithTitle:@""
                                              style:UIBarButtonItemStylePlain
                                              target:self
                                              action:@selector(back)];

- (void)back
{
    [self performSegueWithIdentifier:@"ExitEventOverview" sender:self];
}

这样对吗?

现在下一步是,我需要打开另一个视图来编辑在前一个视图中显示为 table 的事件的详细信息。当我单击 table 行时,编辑视图应该打开,我需要传递事件对象。我如何使用 segues 做到这一点? 完成编辑后,如何以编程方式将取消和保存按钮添加到编辑视图中 return 到所有事件的概览中?

您的编码没问题,还可以添加以下行

UIBarButtonItem *leftBackToPreviousScreenButton = [[UIBarButtonItem alloc]
                                          initWithTitle:@""
                                          style:UIBarButtonItemStylePlain
                                          target:self
                                          action:@selector(back)];

// add this line 
self.navigationItem.rightBarButtonItem = leftBackToPreviousScreenButton;

- (void)back
{
// if you want to pop on previous view controller

 [self.navigationController popViewControllerAnimated:YES];

}

在最后一个问题中你需要自定义,但是如果你需要在两个视图控制器之间传递数据使用这个link

不要忘记实现 UINavigationController 并将带有 4 个按钮的 UIViewController 分配为 root view controller。 Show segues 显示导航栏,其他的不显示,这时你需要管理它并添加它。

要显示详细信息,只需覆盖方法 prepareForSegue:sender 即可:

[self performSegueWithIdentifier:@"NextSegue" sender:nil];

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"NextSegue"]) {
        NextViewController *nextViewController = (NextViewController*)[segue destinationViewController];
        nextViewController.details = self.details;
        // nextViewController.details is the property "details" declared in your NextViewController.h file, you just give it the value you want to send from your present one.
    }
}