解雇ModalViewController 并传回数据

解雇ModalViewController 并传回数据

问题描述:

我有两个视图控制器,firstViewControllersecondViewController.我正在使用此代码切换到我的 secondViewController(我还向它传递了一个字符串):

I have two view controllers, firstViewController and secondViewController. I am using this code to switch to my secondViewController (I am also passing a string to it):

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

然后我在 secondViewController 中使用此代码切换回 firstViewController:

I then use this code in secondViewController to switch back to the firstViewController:

[self dismissModalViewControllerAnimated:YES];

所有这些都可以正常工作.我的问题是,如何将数据传递给 firstViewController?我想将不同的字符串从 secondViewController 传递到 firstViewController.

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.

你需要使用委托协议......这是如何做到的:

You need to use delegate protocols... Here's how to do it:

在你的 secondViewController 的头文件中声明一个协议.它应该是这样的:

Declare a protocol in your secondViewController's header file. It should look like this:

#import <UIKit/UIKit.h>

@protocol SecondDelegate <NSObject>
-(void)secondViewControllerDismissed:(NSString *)stringForFirst
@end


@interface SecondViewController : UIViewController
{
    id myDelegate;  
}

@property (nonatomic, assign) id<SecondDelegate>    myDelegate;

不要忘记在您的实现 (SecondViewController.m) 文件中合成 myDelegate:

Don't forget to synthesize the myDelegate in your implementation (SecondViewController.m) file:

@synthesize myDelegate;

在你的 FirstViewController 的头文件中,通过这样做订阅 SecondDelegate 协议:

In your FirstViewController's header file subscribe to the SecondDelegate protocol by doing this:

#import "SecondViewController.h"

@interface FirstViewController:UIViewController <SecondDelegate>

现在,当您在 FirstViewController 中实例化 SecondViewController 时,您应该执行以下操作:

Now when you instantiate SecondViewController in FirstViewController you should do the following:

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

最后,在您的第一个视图控制器 (FirstViewController.m) 的实现文件中,为 secondViewControllerDismissed 实现 SecondDelegate 的方法:

Lastly, in the implementation file for your first view controller (FirstViewController.m) implement the SecondDelegate's method for secondViewControllerDismissed:

- (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 是实现此目的的另一种方法,但作为最佳实践,当我想跨多个 viewController 或对象进行通信时,我更喜欢使用它.如果您对使用 NSNotifications 感到好奇,这是我之前发布的答案:从appdelegate 中的一个线程跨多个视图控制器触发事件​​

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

如果你想传递多个参数,dismiss前的代码是这样的:

If you want to pass multiple arguments, the code before dismiss looks like this:

if([self.myDelegate respondsToSelector:@selector(secondViewControllerDismissed:argument2:argument3:)])
{
    [self.myDelegate secondViewControllerDismissed:@"THIS IS THE STRING TO SEND!!!" argument2:someObject argument3:anotherObject];
}
[self dismissModalViewControllerAnimated:YES];

这意味着你的 firstViewController 中的 SecondDelegate 方法实现现在看起来像:

This means that your SecondDelegate method implementation inside your firstViewController will now look like:

- (void) secondViewControllerDismissed:(NSString*)stringForFirst argument2:(NSObject*)inObject1 argument3:(NSObject*)inObject2
{
    NSString thisIsTheDesiredString = stringForFirst;
    NSObject desiredObject1 = inObject1;
    //....and so on
}