简体   繁体   English

如何访问父视图控制器?

[英]How to access the parent view controller?

I am using this code to push a view controller in ViewControllerA : 我正在使用此代码在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. 在滑入的DatePickerViewController中,它具有一个文本字段。 In ViewControllerA how can I get the value of this text field once the viewWillDisappear method is fired, it's to save data. ViewControllerA ,一旦触发viewWillDisappear方法,该如何保存此文本字段的值,即保存数据。

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.h

@protocol DatePickerViewControllerDelegate
    -(void)datePicker:(DatePickerViewController *)picker pickedDate:(NSDate *)date;
@end

DatePickerViewController.m DatePickerViewController.m

- (void)viewWillDisappear:(BOOL)animated {
    [self.delegate datePicker:self pickedDate:theDate]; //Delegate is an ivar
    [super viewWillDisappear:animated];
}

ViewControllerA.m ViewControllerA.m

//
DatePickerViewController *viewController = [[DatePickerViewController alloc] initWithNibName:@"DatePickerViewController" bundle:nil];
NSMutableArray *info = [[NSMutableArray alloc] initWithObjects:@"Exam Duration",nil];
[viewController setInfo:info];
[info release];
[viewController setDelegate:self]; // Set the delegate of the date picker
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
//

-(void)datePicker:(DatePickerViewController *)picker pickedDate:(NSDate *)date; {
     //Deal with the new date here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果它们之间有容器视图,如何访问父视图控制器? - How to access parent view controller if there is container view inbetween? 访问模态视图 controller parent - Access modal view controller parent 如何从子视图控制器访问父视图控制器的视图? - how can i access a parent view controller's view from a child view controller? 从父 iOS 访问容器视图控制器 - Access Container View Controller from Parent iOS 如何快速从父视图控制器访问容器视图中的数据? - How can I access data from a container view from the parent view controller in swift? 在弹出视图中从 tableViewController 访问父视图 Controller - Access Parent View Controller from tableViewController in popover view 如何在标签栏控制器中从视图控制器中消除父视图? - How to dismiss parent view from View Controller in Tab Bar Controller? 如何在视图控制器中创建后退按钮以转到父视图控制器 - How to create a back button in a view controller to go to parent view controller 如何关闭视图控制器并返回其父视图控制器? - How to dismiss a view controller and return to it's parent view controller? 如何将模态视图控制器中的触摸发送到父视图控制器? - How to send touches in modal view controller to parent view controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM