prepareForSegue和popViewController的兼容性 [英] prepareForSegue and popViewController compatibility

查看:122
本文介绍了prepareForSegue和popViewController的兼容性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 iOS 应用程序,我连续3次 ViewControllers

I'm working on a iOS app and i have 3 consecutive ViewControllers:

TableViewController - > DetailViewController - > ImageViewController

TableViewController --> DetailViewController --> ImageViewController

我执行de forward使用按钮进行搜索(只需控制拖动故事板)然后返回我有一个自定义后退按钮

I perform de forward Segue with a button (Just control drag on Storyboard) and to go back I have a custom back button with

[self.navigationController popViewControllerAnimated:YES];

要发送数据我使用

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    [segue.destinationViewController someFunction];
}

将数据发送到父 ViewController 我可以在 DetailViewController 中使用 prepareForSegue ,但它在<$ c $中不起作用c> ImageViewController 我必须使用通知

To send data to the parent ViewController I can use prepareForSegue in the DetailViewController, but it doesn't work in the ImageViewController and there I have to use Notifications.

这是好的,如果我使用 prepareForSegue 发送数据 popViewControllerAnimated

It's OK if I use prepareForSegue to send data with popViewControllerAnimated?

我是否应该在两种情况下使用通知?

我在 DetailViewController 中有一个按钮,用于对ImageViewController执行Segue(只需控制拖动 Storyboard )和一个后退按钮:

What I have in the DetailViewController is a button that perform the Segue to the ImageViewController (just control drag on Storyboard) and a Back Button with:

- (IBAction)backAction:(id)sender {
     [self.navigationController popViewControllerAnimated:YES];
}

然后功能:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:@"forwardSegue"]) {
        [segue.destinationViewController someFuntionToImageViewController];
    } else {
        [segue.destinationViewController someFuntionToParentViewController];
    }
}

我注意到我无法指定一个segue。 popViewController 操作的标识符。

I noticed that I can't assign a segue.identifier to the popViewController action.

推荐答案

Apple推荐以下方式在segues导航的视图控制器之间传递数据:

Apple recommends the following way to pass data between view controllers that are navigated by segues:

向前传递数据

您在目标vc上声明属性,并在 prepareForSegue 方法中设置其值。 (所以你这样做的方式)

You declare a property on the destination vc and set its value in the prepareForSegue method. (So kind of the way you did it)

@interface SomeViewController

@property (strong, nonatomic) VeryImportantData *data;
//...
@end

@implementation SourceVC

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    VeryImportantData *theData = ...;
    segue.destinationViewController.data = theData;
}

要传回数据

您声明了一个委托协议。这看起来像这样:

You declare a delegate protocol. This could look like this:

@protocol SomeViewControllerDelegate
- (void)someViewController:(SomeViewController *) someVC didFinishWithData:(SomeData *) data;
@end

您的目的地vc提供委托财产:

Your destination vc provides a delegate property:

@property (weak, nonatomic) id<SomeViewControllerDelegate> delegate;

目标vc在准备好关闭后调用它的委托方法:

The destination vc calls it's delegates method once it's ready to be closed:

@implementation SomeViewController

- (void) close
{
    [delegate someViewController:self didFinishWithData:self.data];
}

您的源控制器实现协议并将自身设置为目标vc的委托在 prepareForSegue 方法中。

And your source controller implements the protocol and sets itself as delegate of the destination vc in the prepareForSegue method.

@implementation SourceVC

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    segue.destinationViewController.delegate = self;
}


//...
- (void)someViewController:(SomeViewController *) someVC didFinishWithData:(SomeData *) data
{
    self.receivedData = data;
    [self.navigationController popViewControllerAnimated:YES];
}

这篇关于prepareForSegue和popViewController的兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆