xcode 反转 Segue 时如何将信息传回 iOS?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13586123/
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 02:11:47  来源:igfitidea点击:

How to Pass information Back in iOS when reversing a Segue?

iosxcodenavigation

提问by user1797508

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.

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

What's the best way to do this?

做到这一点的最佳方法是什么?

Thanks!

谢谢!

回答by Sam Clewlow

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?

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

To implement this

为了实现这一点

At the top of your second view controllers .h file, you'll need to declare an @protocolthat 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).

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

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

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

然后在你的第一个视图控制器.h文件中,实现协议

in the .hfile

.h文件中

#import SecondViewController.h

@interface FirstViewController <MyDataDelegate>

//.....

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

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

@implementation

//.... usual methods

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

    //Do something with data here
}

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

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

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

[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

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

回答by Abizern

If you are using iOS 6 you can use UnwindSegues to return information back down the stack.

如果您使用的是 iOS 6,您可以使用 UnwindSegues 将信息返回到堆栈中。

回答by Phillip Mills

Probably the simplest way is by using NSNotification. In your prepareForSegue:have VC One listen for a custom notification from VC Two. When VC Two wants to send the array -- perhaps in its viewWillDisappear-- it posts that notification and passes the array as the notification object. VC One is notified, at which point is stops listening and uses the array.

可能最简单的方法是使用NSNotification. 在您的prepareForSegue:VC One 中,侦听来自 VC 二的自定义通知。当 VC Two 想要发送数组时——也许在它的里面viewWillDisappear——它会发布该通知并将该数组作为通知对象传递。VC One 收到通知,此时停止侦听并使用该阵列。