iOS 6 – 我可以返回数据,当我解开一个segue?

我使用故事板工具创build了一个简单的放松继续。 我在视图中创build了以下事件处理程序,我想解开:

-(IBAction)quitQuiz:(UIStoryboardSegue *)segue { NSLog(@"SEGUE unwind"); } 

这会正确触发并解开赛格(消息被logging)。

当用户退出测验时,我想通过一些数据,并一直在努力如何实现这一目标。 任何人都可以build议吗?

谢谢杰夫。 看完WWDCvideo407后,我有一个明确的解决scheme。

在作为展开目标的视图控制器中,应该创build一个方法,该方法只需要一个UIStoryboardSegue参数并返回一个IBAction。 UIStoryboardSegue有一个方法来返回源视图控制器! 这是从video中取得的例子(信贷给苹果公司)。

 - (IBAction)done:(UIStoryboardSegue *)segue { ConfirmationViewController *cc = [segue sourceViewController]; [self setAccountInfo:[cc accountInfo]]; [self setShowingSuccessView:YES]; } 

在这个苹果演讲中,从演讲的后半部分(编辑:从37:20开始)

特别是,在展开过程中,[segue sourceViewController]是展开事件所依赖的视图控制器,所以只需像往常一样访问属性即可。

在closures的控制器中添加函数prepareForSeque。

– (void) prepareForSegue 🙁 UIStoryboardSegue *)segue sender:(id) sender

这个函数在unwind segue被调用之前被调用(在你的例子中你叫它quitQuiz)。 正如你所看到的,它也有一个sender参数,这样你就可以找出谁叫unwind,并相应地收集相关的数据。

例如WWDC 407的video,如果你点击重置button,你不会设置accountInfo,如果你点击完成button,你会。

设置一个委托,通知你的源视图控制器退出测验并发回数据。 不要忘记将源视图控制器设置为目标视图控制器的委托。

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

是的
为此,您需要创build属性,以保存从另一个视图控制器发送的数据:

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

在视图控制器之间传递数据通常使用协议完成。 这是一个例子:

在您的测验视图控制器标题中,声明一个类似的协议定义:

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

在呈现视图控制器的prepareForSeque:方法中,连接prepareForSeque:

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

然后,在你的呈现视图控制器中,处理委托协议的quizController:didQuitWithState:方法。

最后,一旦用户退出测验,您应该使用协议通知代理,传递状态或任何您想要公开的数据。