ios - uipopover 委托(delegate)中的 uidatepicker

标签 ios uiviewcontroller delegates uidatepicker uipopover

我正在尝试使用两个按钮在其中插入带有 uidatepicker 的 uipopover - 取消和完成。在 UIDatePicker in UIPopover 的帮助下,我能够使用 Storyboard设计布局和
How can I show a UIDatePicker inside a Popover on iPad using StoryBoard?

但是,当按下完成按钮时,我无法从 uidatepicker 到 uiviewcontroller 获取日期。我是 ios 编程的新手,我发现委托(delegate)方法非常困惑。有人可以解释一下吗?
非常感谢帮助。谢谢 :)

enter image description here

最佳答案

在 View Controller 中获取值的一种绝对正确的方法是实现委托(delegate)函数。如果您在技术层面上理解,这些都非常容易。我会在这里尝试解释一下。

您必须像这样在 datePickerViewcontrollerClass.h 中定义协议(protocol)

@protocol TimePopupViewControllerDelegate <NSObject> 
-(void)returnSelectedDate:(NSDate*)date;
@end


并创建一个“id”类型的实例,以像这样传递 mainViewController 的引用。

@property (nonatomic, assign) id < TimePopupViewControllerDelegate > delegate;



在您创建 datePickerViewcontrollerClass 实例的 MainViewController.m 中,您必须像这样设置委托(delegate)

datePickerViewcontrollerClass *myViewControllerForPopover =[[datePickerViewcontrollerClass alloc] init];
myViewControllerForPopover.delegate = self;


在从 datePickerViewcontrollerClass.m 类中的选择器获取日期的方法中,您必须使用委托(delegate)将其传递给主类。

-(void)viewDidDisappear:(BOOL)animated{
     [_delegate returnSelectedDate:datepicker.date];
[super viewWillDisappear:animated];

}



您可以使用我在 ViewWillDisappear 中编写的任何方法或任何其他方法来编写它。

在此之后 MainViewController 调用此方法,您可以检索所选日期

-(void)returnSelectedDate:(NSDate *)date{
}


从技术上讲,您将 mainViewController 实例的引用传递给您的 datePickerViewcontrollerClass 并从 datePickerViewcontrollerClass 调用 mainViewController 上的方法
如果您仍有任何疑问,我希望我能够清楚地解释它,您可以发表评论。

关于ios - uipopover 委托(delegate)中的 uidatepicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17628104/

相关文章:

ios - 为什么自定义委托(delegate)协议(protocol)包含可选值 - SWIFT

iphone - 有没有一种方法可以定义某些类可以访问而其他类不能访问的变量?

ios - Popover TableView 未显示选中标记 (iOS7.1)

ios - 在 UITabBar 的每个 UINavigationController 中重新加载所有 UIViewController

javascript - 为什么不委托(delegate)滚动工作?

ios - 使用委托(delegate)从模态视图 Controller 调用另一个 View Controller 中的方法

ios - 首次启动后应用程序崩溃

ios - Xcode - 使用导航栏按钮项和 Storyboard更改 View

ios - 仅在一个 ViewController 中旋转

objective-c - UINavigationController 上未出现后退按钮