How to Pass Data Back [duplicate]

2019-09-20 06:53发布

This question already has an answer here:

This is the scene of my storyboard.

UINavigationController -> UITableViewController -> push segue -> UIViewController

I know how to pass the data of UITableViewController to UIViewController. But, how can I pass the data back to the UITableViewController?(UIViewController has a navigation bar with a back button)

1条回答
冷血范
2楼-- · 2019-09-20 07:50

You can do it through the use of delegation.

When you pass data of UITableViewController to UIViewController, you can set a delegate on UIViewController. Whenever you want to pass data back to UITableViewController, you can call the delegate and pass in the data

Here's a more concrete example in code

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller
        UIViewController *vc = [segue destinationViewController];

        // Pass the UITableViewController to the UIViewController
        [vc setDelegate:self];
    }
}

Create a method on UITableViewController that takes data from UIViewController (example: -(void)processDataFromUIViewController:(NSString *)string)

Now when you want to pass data back to UITableViewController, call [delegate sendDataFromUIViewController:@"data"]

查看更多
登录 后发表回答