如何访问父视图控制器?
我正在使用此代码在 ViewControllerA 中推送视图控制器:
DatePickerViewController *viewController = [[DatePickerViewController alloc] initWithNibName:@"DatePickerViewController" bundle:nil];
NSMutableArray *info = [[NSMutableArray alloc] initWithObjects:@"Exam Duration",nil];
[viewController setInfo:info];
[info release];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
在滑入的 DatePickerViewController 中,它有一个文本字段。在 ViewControllerA 中,一旦触发 viewWillDisappear 方法,如何获取此文本字段的值,它是为了保存数据。
I am using this code to push a view controller in ViewControllerA
:
DatePickerViewController *viewController = [[DatePickerViewController alloc] initWithNibName:@"DatePickerViewController" bundle:nil];
NSMutableArray *info = [[NSMutableArray alloc] initWithObjects:@"Exam Duration",nil];
[viewController setInfo:info];
[info release];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
In the DatePickerViewController
that is slid in, it has a text field. In ViewControllerA
how can I get the value of this text field once the viewWillDisappear
method is fired, it's to save data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几种方法可以做到这一点。最好是使用委托模式或通知系统。
您选择哪一种取决于您的愿望。如果您只想让“父控制器”了解该值,请使用委托方法。
使用委托或通知创建松散耦合的结构并使可重用的类成为可能。
编辑:一些示例代码。当心:未经测试,来自我的头脑!
DatePickerViewController.h
DatePickerViewController.m
ViewControllerA.m
There are several ways to do this. Best would be to use a delegate pattern or a notification system.
Which one you choose depends on your whishes. If you just want the 'parent controller' to know about the value, use a delegate method.
Using delegates or notifications creates a loosely coupled structure and makes reusable classes possible.
EDIT: Some sample code. Beware: untested and from the top of my head!
DatePickerViewController.h
DatePickerViewController.m
ViewControllerA.m