xcode iOS 6 - 我可以在展开 segue 时返回数据吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13038622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:54:38  来源:igfitidea点击:

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

iphoneobjective-ciosxcodesegue

提问by Mark Tyers

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?

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

回答by Mark Tyers

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

谢谢杰夫。观看WWDC 视频 407 后,我有了明确的解决方案。

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).

在作为展开目标的视图控制器中,您应该创建一个方法,该方法采用单个 UIStoryboardSegue 参数并返回一个 IBAction。UIStoryboardSegue 有一个方法可以返回源视图控制器!这是视频中的示例(归功于 Apple)。

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

回答by Jeff

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)

在这个苹果演讲中,演示文稿的后半部分很好地解释了从 unwind segue 中取回数据(编辑:从 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.

特别是,在 unwind segue 中, [segue sourceViewController] 是 unwind 事件源自的仍然活动的视图控制器,因此只需像往常一样访问您的属性。

回答by Yasmin Tiomkin

Add the function prepareForSeque in the controller being closed.

在正在关闭的控制器中添加函数 prepareForSeque。

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

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

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

在调用unwindsegue之前调用此函数(在您的示例中,您将其称为 quitQuiz)。如您所见,它还有一个sender参数,以便您可以找出谁调用了unwind 并相应地收集相关数据。

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

例如 WWDC 407 视频,如果您单击重置按钮,您将不会设置 accountInfo,如果您单击完成按钮,您将设置。

回答by Nitesh Borad

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

回答by Mundi

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

回答by Jesse Bunch

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:

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

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

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

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

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.

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