简体   繁体   English

performSegueWithIdentifier和sharedData

[英]performSegueWithIdentifier and sharedData

I hope I'm not asking something that's been already answered (but I found no answer to this, so hopefully I'm not). 我希望我不会问一些已经回答过的问题(但我没有找到答案,所以希望我不是)。

I have an app in the current xcode version, using segues and navigationController. 我在当前的xcode版本中有一个应用程序,使用segues和navigationController。 I need to pass data from one view to the other - what's the easiest way to do this? 我需要将数据从一个视图传递到另一个视图 - 最简单的方法是什么? I ran onto some sharedData thing that could be possibly hooked onto the performSegueWithIdentifier method but don't know how to use it (or whether it is the right choice to do it like this). 我遇到了一些可能挂在performSegueWithIdentifier方法上的sharedData东西,但不知道如何使用它(或者是否正确选择这样做)。

Thanks 谢谢

A segue has two view controllers: sourceViewController and destinationViewController . segue有两个视图控制器: sourceViewControllerdestinationViewController When UIKit executes a segue, it sends a prepareForSegue:sender: message to the source VC. 当UIKit执行segue时,它会向源VC发送prepareForSegue:sender:消息。 You can override that method in your view controller subclass to pass data to the destination VC. 您可以在视图控制器子类中覆盖该方法,以将数据传递到目标VC。

For example, suppose you have a master view controller with a table view of movies, and when the user clicks a row in the table view, you want to segue to a detail view controller for the movie. 例如,假设您有一个主视图控制器,其中包含电影的表视图,当用户单击表视图中的一行时,您希望转换为电影的详细视图控制器。

@implementation MasterViewController

...

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    DetailViewController *detailVC = segue.destinationViewController;
    NSIndexPath *selectedPath = [self.tableView indexPathForSelectedRow];
    detailVC.movie = [self movieForIndexPath:selectedPath];
}

This is explained in the Introducing Interface Builder Storyboarding video from WWDC 2011. 这在WWDC 2011的Introducing Interface Builder Storyboarding视频中进行了解释。

It's also worth noting that when the segue's origin is a table view cell, or the accessory button of a table view cell, the sender argument of prepareForSegue:sender: is the table view cell. 值得注意的是,当segue的原点是表格视图单元格或表格视图单元格的附件按钮时, prepareForSegue:sender:sender参数是表格视图单元格。

I think the best way is to import header for the view controller that will be shown in controller that is performing segue. 我认为最好的方法是导入将在执行segue的控制器中显示的视图控制器的标头。 And then use it's accessors or other methods to pass needed data inside prepareForSegue: 然后使用它的访问器或其他方法在prepareForSegue中传递所需的数据:

// In FirstViewController.h

#import "SecondViewController.h"

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"SegueToSecondViewController"]) {
        // Get destination view controller and don't forget
        // to cast it to the right class
        SecondViewController *secondController = [segue destinationViewController];
        // Pass data
        secondController.dataArray = self.someDataArray;
        secondController.name = @"Fancy name";
    }
}

When you want data back from second to first, I suggest to use delegate: 当你想要数据从第二个到第一个时,我建议使用委托:

// In FirstViewController.h

#import "SecondViewController.h"
#import "SecondViewControllerDelegate.h"

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"SegueToSecondViewController"]) {

        SecondViewController *secondController = [segue destinationViewController];

        // Declare first view controller as a delegate
        secondController.delegate = self;

        // Pass data
        secondController.dataArray = self.someDataArray;
        secondController.name = @"Fancy name";
    }
}

// Second controller's delegate method,controller
// ie. used to return data after second view is dismissed
- (void)secondControllerFinishedSomeTask:(NSArray *)someReturnedData {
    // Do something with returned data
}

当您希望数据从第二个返回到第一个时,更好的方法是使用Unwind Segues

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM