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

假如想象 提交于 2019-11-26 22:45:36

问题


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)quitQuiz:(UIStoryboardSegue *)segue {
    NSLog(@"SEGUE unwind");
}

This fires correctly and unwinds the segue (the message gets logged).

When the user quits the quiz I would like to pass some data back and have been struggling with how to achieve this. Can anyone advise?


回答1:


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];
}



回答2:


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.




回答3:


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.




回答4:


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;
}



回答5:


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];
        }
    }



回答6:


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.



来源:https://stackoverflow.com/questions/13038622/ios-6-can-i-return-data-when-i-unwind-a-segue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!