我有两个 View Controller ,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];
然后我在 secondViewController 中使用这段代码切换回 firstViewController:
[self dismissModalViewControllerAnimated:YES];
一切正常。我的问题是,如何将数据传递给 firstViewController?我想将另一个字符串从 secondViewController 传递到 firstViewController。
最佳答案
您需要使用委托(delegate)协议(protocol)...以下是操作方法:
在您的 secondViewController 的头文件中声明一个协议(protocol)。它应该看起来像这样:
#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:
@synthesize myDelegate;
在您的 FirstViewController 的头文件中订阅 SecondDelegate 协议(protocol):
#import "SecondViewController.h"
@interface FirstViewController:UIViewController <SecondDelegate>
现在,当您在 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];
最后,在您的第一个 View Controller (FirstViewController.m) 的实现文件中,为 secondViewControllerDismissed 实现 SecondDelegate 的方法:
- (void)secondViewControllerDismissed:(NSString *)stringForFirst
{
NSString *thisIsTheDesiredString = stringForFirst; //And there you have it.....
}
现在,当您要关闭第二个 View Controller 时,您想要调用第一个 View Controller 中实现的方法。这部分很简单。你所要做的就是,在你的第二个 View Controller 中,在关闭代码之前添加一些代码:
if([self.myDelegate respondsToSelector:@selector(secondViewControllerDismissed:)])
{
[self.myDelegate secondViewControllerDismissed:@"THIS IS THE STRING TO SEND!!!"];
}
[self dismissModalViewControllerAnimated:YES];
委托(delegate)协议(protocol)非常、非常、非常有用。熟悉它们对你有好处:)
NSNotifications 是实现此目的的另一种方式,但作为最佳实践,当我想跨多个 viewController 或对象进行通信时,我更喜欢使用它。如果您对使用 NSNotifications 感到好奇,这是我之前发布的答案:Firing events accross multiple viewcontrollers from a thread in the appdelegate
编辑:
如果要传递多个参数,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];
这意味着您的 SecondDelegate 方法在您的 firstViewController 中的实现现在看起来像:
- (void) secondViewControllerDismissed:(NSString*)stringForFirst argument2:(NSObject*)inObject1 argument3:(NSObject*)inObject2
{
NSString thisIsTheDesiredString = stringForFirst;
NSObject desiredObject1 = inObject1;
//....and so on
}
关于iphone - 关闭模态视图 Controller 并将数据传回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6203799/