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

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

问题描述

我有两个视图控制器,一个和两个。我从VC One转到VC Two。在VC Two上,我选择了一些存储在数组中的数据。当我按导航栏上的后退按钮时,我想将该阵列发送回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.

最好的方法是什么?

谢谢!

推荐答案

为什么不在你的第二个上设置一个委托属性查看控制器,第一个可以注册为。然后当信息存储到数组时,它也可以传递回它的委托?

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 文件,在 .h 文件中实现协议

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

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

为了将第一个视图控制器设置为委托,你需要先截取segue它通过使用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天全站免登陆