如何访问父视图控制器?

发布于 2024-08-31 11:50:45 字数 554 浏览 3 评论 0原文

我正在使用此代码在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画尸师 2024-09-07 11:50:45

有几种方法可以做到这一点。最好是使用委托模式或通知系统。

您选择哪一种取决于您的愿望。如果您只想让“父控制器”了解该值,请使用委托方法。

使用委托或通知创建松散耦合的结构并使可重用的类成为可能。

编辑:一些示例代码。当心:未经测试,来自我的头脑!

DatePickerViewController.h

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

DatePickerViewController.m

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

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
}

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

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

DatePickerViewController.m

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

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文