在视图控制器中获取值的一种方法是实现委托函数,这是绝对正确的。如果您在技术层面上理解,这些都非常容易。我会在这里尝试解释一下。
你必须像这样在 datePickerViewcontrollerClass.h 中定义协议
@protocol TimePopupViewControllerDelegate <NSObject>
-(void)returnSelectedDate:(NSDate*)date;
@end
并创建一个 'id' 类型的实例,以像这样传递 mainViewController 的引用。
@property (nonatomic, assign) id 委托;
在您创建 datePickerViewcontrollerClass 实例的 MainViewController.m 中,您必须像这样设置委托
datePickerViewcontrollerClass *myViewControllerForPopover =[[datePickerViewcontrollerClass alloc] init];
myViewControllerForPopover.delegate = self;
在从 datePickerViewcontrollerClass.m 类中的选择器获取日期的方法中,您必须使用委托将其传递给主类。
-(void)viewDidDisappear:(BOOL)animated{
[_delegate returnSelectedDate:datepicker.date];
[super viewWillDisappear:animated];
}
您可以使用我在 ViewWillDisappear 中编写的任何方法或任何其他方法来编写它。
在 MainViewController 中调用此方法后,您可以检索所选日期
-(void)returnSelectedDate:(NSDate *)date{
}
从技术上讲,您将 mainViewController 实例的引用传递给您的 datePickerViewcontrollerClass 并从 datePickerViewcontrollerClass 调用 mainViewController 上的方法
如果您还有任何疑问可以发表评论,我希望我能够解释清楚。