iOS 6 - can i return data when i unwind a segue?

前端 未结 6 382
臣服心动
臣服心动 2020-12-05 03:09

I have created a simple unwind segue using the storyboard tools. I have created the following event handler in the view I want to unwind to:

-(IBAction)quitQ         


        
相关标签:
6条回答
  • 2020-12-05 03:19

    Set up a delegate and inform your source view controller about quitting the quiz and send back the data. Don't forget to set the source view controller as the delegate of the destination view controller.

    // DestinationViewController.h
    @protocol DestingationDelegate;
    @interface 
    ...
    @property (assign) id<DestinationDelegate> delegate;
    ...
    @end
    
    @protocol DestinationDelegate
    -(void)didQuitQuiz:(NSDictionary*)infoDict;
    @end
    
    // DestinationViewController.m
    -(IBAction)quitQuiz:(UIStoryboardSegue *)segue {
      NSLog(@"SEGUE unwind");
      if (self.delegate) [self.delegate didQuitQuiz:infoDict];
    }
    
    
    // SourceViewController.h
    #import DestinationViewController.h
    @interface SourceViewController : ViewController <DestinationDelegate>
    ....
    
    // SourceViewController.m
    -(void)didQuitQuiz:(NSDictionary *)infoDict {
        if (infoDict) {
           // do something with the data
        }
    }
    
    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
       ...
       destinationViewController.delegate = self;
    }
    
    0 讨论(0)
  • 2020-12-05 03:23

    Add the function prepareForSeque in the controller being closed.

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

    This function is called before the unwind segue is called (in your example you called it quitQuiz). As you can see, it also has a sender parameter so that you can find out who called the unwind and collect the relevant data accordingly.

    For example of the WWDC 407 video, if you clicked the reset button you would not set the accountInfo and if you clicked the done button you would.

    0 讨论(0)
  • 2020-12-05 03:28

    Passing data between view controllers is frequently accomplished using protocols. Here's an example:

    In your quiz view controller header, declare a similar protocol definition:

    @protocol JBQuizViewControllerDelegate <NSObject>
    
    @required
    - (void)quizController:(id)controller didQuitWithState:(NSString *)state;
    
    @end
    

    In your presenting view controller's prepareForSeque: method, wire up the delegate:

    JBQuizViewController *destination = (JBQuizViewController *)segue.destinationViewController;
    destination.delegate = self;
    

    Then, in your presenting view controller, handle the delegate protocol's quizController:didQuitWithState: method.

    Finally, once the user quits your quiz, you should notify the delegate using the protocol, passing in the state or whatever data you want to expose.

    0 讨论(0)
  • 2020-12-05 03:34

    Getting data back from an unwind segue is explained very nicely in this apple talk, second half of the presentation (edit: starts from 37:20)

    In particular, in an unwind segue the [segue sourceViewController] is the still active view controller from which the unwind event originated, so just access your properties as usual.

    0 讨论(0)
  • 2020-12-05 03:35

    Yes,
    For that, you will need to make properties, which holds your data to be sent from another view controller:

        - (IBAction)unwindSelectFriendsVC:(UIStoryboardSegue *)segue
        {
            if ([segue.sourceViewController isKindOfClass:[ChildVC class]]) {
    
                ChildVC *child = (ChildVC *) segue.sourceViewController;
    
                //here we are passing array of selected friends by arrSelectedFriends property
                self.arrFriendList = child.arrSelectedFriends;
                [self.tableView reloadData];
            }
        }
    
    0 讨论(0)
  • 2020-12-05 03:37

    Thanks Jeff. After watching WWDC video 407 I have a clear solution.

    In the view controller that is the target of the unwind you should create a method that takes a single UIStoryboardSegue parameter and returns an IBAction. The UIStoryboardSegue has a method to return the source view controller! Here is the example taken from the video (credit to Apple).

    - (IBAction)done:(UIStoryboardSegue *)segue {
        ConfirmationViewController *cc = [segue sourceViewController];
        [self setAccountInfo:[cc accountInfo]];
        [self setShowingSuccessView:YES];
    }
    
    0 讨论(0)
提交回复
热议问题