ios 解雇ModalViewController 并传回数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6203799/
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
dismissModalViewController AND pass data back
提问by Andrew Davis
I have two view controllers, firstViewControllerand secondViewController. I am using this code to switch to my secondViewController (I am also passing a string to it):
我有两个视图控制器,firstViewController和secondViewController。我正在使用此代码切换到我的 secondViewController (我还向它传递了一个字符串):
secondViewController *second = [[secondViewController alloc] initWithNibName:nil bundle:nil];
second.myString = @"This text is passed from firstViewController!";
second.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:second animated:YES];
[second release];
I then use this code in secondViewController to switch back to the firstViewController:
然后我在 secondViewController 中使用此代码切换回 firstViewController:
[self dismissModalViewControllerAnimated:YES];
All of this works fine. My question is, how would I pass data to the firstViewController? I would like to pass a different string into the firstViewController from the secondViewController.
所有这些工作正常。我的问题是,如何将数据传递给 firstViewController?我想将不同的字符串从 secondViewController 传递到 firstViewController。
回答by Sid
You need to use delegate protocols... Here's how to do it:
您需要使用委托协议......这是如何做到的:
Declare a protocol in your secondViewController's header file. It should look like this:
在你的 secondViewController 的头文件中声明一个协议。它应该是这样的:
#import <UIKit/UIKit.h>
@protocol SecondDelegate <NSObject>
-(void)secondViewControllerDismissed:(NSString *)stringForFirst
@end
@interface SecondViewController : UIViewController
{
id myDelegate;
}
@property (nonatomic, assign) id<SecondDelegate> myDelegate;
Don't forget to synthesize the myDelegate in your implementation (SecondViewController.m) file:
不要忘记在您的实现 (SecondViewController.m) 文件中合成 myDelegate:
@synthesize myDelegate;
In your FirstViewController's header file subscribe to the SecondDelegate protocol by doing this:
在您的 FirstViewController 的头文件中,通过执行以下操作订阅 SecondDelegate 协议:
#import "SecondViewController.h"
@interface FirstViewController:UIViewController <SecondDelegate>
Now when you instantiate SecondViewController in FirstViewController you should do the following:
现在,当您在 FirstViewController 中实例化 SecondViewController 时,您应该执行以下操作:
// If you're using a view controller built with Interface Builder.
SecondViewController *second = [[SecondViewController alloc] initWithNibName:"SecondViewController" bundle:[NSBundle mainBundle]];
// If you're using a view controller built programmatically.
SecondViewController *second = [SecondViewController new]; // Convenience initializer that uses alloc] init]
second.myString = @"This text is passed from firstViewController!";
second.myDelegate = self;
second.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:second animated:YES];
[second release];
Lastly, in the implementation file for your first view controller (FirstViewController.m) implement the SecondDelegate's method for secondViewControllerDismissed:
最后,在您的第一个视图控制器 (FirstViewController.m) 的实现文件中,为 secondViewControllerDismissed 实现 SecondDelegate 的方法:
- (void)secondViewControllerDismissed:(NSString *)stringForFirst
{
NSString *thisIsTheDesiredString = stringForFirst; //And there you have it.....
}
Now when you're about to dismiss the second view controller you want to invoke the method implemented in the first view controller. This part is simple. All you do is, in your second view controller, add some code before the dismiss code:
现在,当您要关闭第二个视图控制器时,您要调用在第一个视图控制器中实现的方法。这部分很简单。你所做的就是,在你的第二个视图控制器中,在关闭代码之前添加一些代码:
if([self.myDelegate respondsToSelector:@selector(secondViewControllerDismissed:)])
{
[self.myDelegate secondViewControllerDismissed:@"THIS IS THE STRING TO SEND!!!"];
}
[self dismissModalViewControllerAnimated:YES];
Delegate protocols are EXTREMELY, EXTREMELY, EXTREMELY useful. It would do you good to familiarize yourself with them :)
委托协议非常、非常、非常有用。熟悉它们对你有好处:)
NSNotifications are another way to do this, but as a best practice, I prefer using it when I want to communicate across multiple viewControllers or objects. Here's an answer I posted earlier if you're curious about using NSNotifications: Firing events accross multiple viewcontrollers from a thread in the appdelegate
NSNotifications 是实现此目的的另一种方法,但作为最佳实践,当我想跨多个 viewController 或对象进行通信时,我更喜欢使用它。如果您对使用 NSNotifications 感到好奇,这是我之前发布的一个答案:从 appdelegate 中的线程跨多个视图控制器触发事件
EDIT:
编辑:
If you want to pass multiple arguments, the code before dismiss looks like this:
如果你想传递多个参数,dismiss前的代码是这样的:
if([self.myDelegate respondsToSelector:@selector(secondViewControllerDismissed:argument2:argument3:)])
{
[self.myDelegate secondViewControllerDismissed:@"THIS IS THE STRING TO SEND!!!" argument2:someObject argument3:anotherObject];
}
[self dismissModalViewControllerAnimated:YES];
This means that your SecondDelegate method implementation inside your firstViewController will now look like:
这意味着你的 firstViewController 中的 SecondDelegate 方法实现现在看起来像:
- (void) secondViewControllerDismissed:(NSString*)stringForFirst argument2:(NSObject*)inObject1 argument3:(NSObject*)inObject2
{
NSString thisIsTheDesiredString = stringForFirst;
NSObject desiredObject1 = inObject1;
//....and so on
}
回答by Lizza
I could be way out of place here, but I am starting to much prefer the block syntax to the very verbose delegate/protocol approach. If you make vc2 from vc1, have a property on vc2 that you can set from vc1 that is a block!
我在这里可能有点不合适,但我开始更喜欢块语法而不是非常冗长的委托/协议方法。如果你从 vc1 生成 vc2,在 vc2 上有一个属性,你可以从 vc1 设置一个块!
@property (nonatomic, copy) void (^somethingHappenedInVC2)(NSString *response);
Then, when something happens in vc2 that you want to tell vc1 about, just execute the block that you defined in vc1!
然后,当 vc2 中发生了您想告诉 vc1 的事情时,只需执行您在 vc1 中定义的块即可!
self.somethingHappenedInVC2(@"Hello!");
This allows you to send data from vc2 back to vc1. Just like magic. IMO, this is a lot easier/cleaner than protocols. Blocks are awesome and need to be embraced as much as possible.
这允许您将数据从 vc2 发送回 vc1。就像魔术一样。IMO,这比协议更容易/更干净。块很棒,需要尽可能多地被拥抱。
EDIT - Improved example
编辑 - 改进的例子
Let's say we have a mainVC that we want to present a modalVC on top of temporarily to get some input from a user. In order to present that modalVC from mainVC, we need to alloc/init it inside of mainVC. Pretty basic stuff. Well when we make this modalVC object, we can also set a block property on it that allows us to easily communicate between both vc objects. So let's take the example from above and put the follwing property in the .h file of modalVC:
假设我们有一个 mainVC,我们想在它上面临时呈现一个 modalVC 以从用户那里获得一些输入。为了从 mainVC 呈现那个 modalVC,我们需要在 mainVC 内部分配/初始化它。很基本的东西。好吧,当我们制作这个 modalVC 对象时,我们还可以在其上设置一个块属性,使我们可以轻松地在两个 vc 对象之间进行通信。因此,让我们从上面的例子,把follwing财产modalVC的.h文件:
@property (nonatomic, copy) void (^somethingHappenedInModalVC)(NSString *response);
Then, in our mainVC, after we have alloc/init'd a new modalVC object, you set the block property of modalVC like this:
然后,在我们的 mainVC 中,在我们分配/初始化一个新的 modalVC 对象之后,您可以像这样设置 modalVC 的 block 属性:
ModalVC *modalVC = [[ModalVC alloc] init];
modalVC.somethingHappenedInModalVC = ^(NSString *response) {
NSLog(@"Something was selected in the modalVC, and this is what it was:%@", response);
}
So we are just setting the block property, and defining what happens when that block is executed.
所以我们只是设置块属性,并定义执行该块时会发生什么。
Finally, in our modalVC, we could have a tableViewController that is backed by a dataSource array of strings. Once a row selection is made, we could do something like this:
最后,在我们的 modalVC 中,我们可以有一个 tableViewController,它由字符串的 dataSource 数组支持。一旦进行了行选择,我们可以执行以下操作:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedString = self.dataSource[indexPath.row];
self.somethingHappenedInModalVC(selectedString);
}
And of course, each time we select a row in modalVC, we are going to get a console output from our NSLog line back in mainVC. Hope that helps!
当然,每次我们在 modalVC 中选择一行时,我们都会从我们的 NSLog 行返回 mainVC 中的控制台输出。希望有帮助!
回答by theiOSDude
hmm, look for the notification centre and pass back info in a notification. here is apples take on it- I take this approach personally unless any one has any other suggestions
嗯,寻找通知中心并在通知中传回信息。这是苹果公司的做法- 我个人采取这种方法,除非有人有任何其他建议
回答by cschwarz
Define a delegate protocol in the second view controller and make the first one the delegate of the second.
在第二个视图控制器中定义一个委托协议,并使第一个成为第二个的委托。