反转Segue时如何将信息传回iOS? [英] How to Pass information Back in iOS when reversing a Segue?

查看:19
本文介绍了反转Segue时如何将信息传回iOS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个视图控制器,一个和两个.我从 VC 1 转到 VC 2.在 VC 2 上,我选择了一些存储在数组中的数据.当我按下导航栏上的返回"按钮时,我想将该数组发送回 VC One.

I have two view controllers, One and Two. I go from VC One to VC Two. On VC Two, I select some data that I store in an array. When I press the "Back" button on the navigation bar, I would like to send that array back to VC One.

最好的方法是什么?

谢谢!

推荐答案

为什么不在你的第二个视图控制器上设置第一个可以注册的委托属性.那么当信息存入数组时,也可以传回给它的delegate?

Why not set up a delegate property on your second view controller that the first can register as. Then when the information is stored to the array, it can also be passed back to it's delegate?

实现这个

在您的第二个视图控制器 .h 文件的顶部,您需要声明第一个视图控制器可以实现的 @protocol.协议类似于其他语言的接口.这是一种确保对象实现某些方法的方法,而无需具体了解该对象是什么(在本例中为视图控制器 1).

At the top of your second view controllers .h file, you'll need to declare an @protocol that the first view controller can implement. A protocol is simular to an interface in other languages. It's a way of being sure an object implements certain methods, without needing to know specifically what that object is (view controller 1 in this case).

@protocol MyDataDelegate

- (void)recieveData:(NSArray *)theData

@end

并且还为委托声明一个属性,第一个视图控制器可以像在呈现第二个视图控制器之前一样设置它自己

and also declare a property for the delegate that the first view controller can set it's self as before it presents the second

@interface SecondViewController

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

然后在您的第一个视图控制器 .h 文件中,按如下方式实现协议

Then in your first view controller .h file, implement the protocol as so

.h 文件中

#import SecondViewController.h

@interface FirstViewController <MyDataDelegate>

//.....

.m中,实现协议中声明的方法

and in the .m, implement the methods declared in the protocol

@implementation

//.... usual methods

- (void)recieveData:(NSArray *)theData {

    //Do something with data here
}

为了将第一个视图控制器设置为委托,您需要使用 UIStoryBoardDelegate 方法在它发生之前拦截它.将此添加到第一个视图控制器

In order to set the first view controller as the delegate, you need to intercept the segue before it happens, by using a UIStoryBoardDelegate method. Add this to the first view controller

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

    //Get a handle on the view controller about be presented
    SecondViewController *secondViewController = segue.destinationViewController;

    if ([secondViewController isKindOfClass:[SecondViewController class]]) {
        secondViewController.delegate = self;
    }
}

现在你有一个指向第二个视图控制器的指针,并且可以通过在第二个视图控制器中调用以下方法来调用方法并传回数据

Now you have a pointer to the first view controller from the second, and can call methods and pass back the data, by calling the following method in the second view controller

[self.delegate recieveData:theArrayData];

如果您愿意,您还可以向协议添加另一个方法来通知委托第二个视图控制器正在被解除.或者使用其他答案中的一些建议

You could also add another method to the protocol to notify the delegate that the second view controller is being dismissed if you wanted. Or use some of the suggestions from the other answers

这篇关于反转Segue时如何将信息传回iOS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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