如何在 IOS8 的另一个视图中使用 UIDatePicker 中的选定数据?
How can I use the selected data from a UIDatePicker in another view in IOS8?
我对 IOS 8 和一般编程还很陌生。我想用 UIDatePicker 的值填充 UIButton 的标签文本。当用户按下按钮时,会出现一个带有日期选择器的弹出窗口。我为我的日期选择器创建了一个嵌入导航控制器的新视图控制器。它们使用 popover segue 连接。我的问题是,一旦用户选择了一个日期,我如何才能将此信息传回原始视图控制器以在按钮标签上显示该值。任何帮助将不胜感激。
像这样使用 segues 在 2 个视图控制器之间传递值更容易:
class VC1:UIViewController {
@IBAction func next() {
let valueToPass = NSDate() // Could be anything that conforms to AnyObject
self.performSegueWithIdentifier("segueID", sender: valueToPass) // Use this method unless you're connecting a button to a viewcontroller in storyboard
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let vc2 = segue.destinationViewController as VC2 // The viewcontroller you're going to
vc2.date = sender as NSDate // If you connected button to vc in storyboard sender will be nil, so in that case just set the vc2.date directly here
}
}
class VC2:UIViewController {
var date:NSDate! // Property you want to pass to
}
我对 IOS 8 和一般编程还很陌生。我想用 UIDatePicker 的值填充 UIButton 的标签文本。当用户按下按钮时,会出现一个带有日期选择器的弹出窗口。我为我的日期选择器创建了一个嵌入导航控制器的新视图控制器。它们使用 popover segue 连接。我的问题是,一旦用户选择了一个日期,我如何才能将此信息传回原始视图控制器以在按钮标签上显示该值。任何帮助将不胜感激。
像这样使用 segues 在 2 个视图控制器之间传递值更容易:
class VC1:UIViewController {
@IBAction func next() {
let valueToPass = NSDate() // Could be anything that conforms to AnyObject
self.performSegueWithIdentifier("segueID", sender: valueToPass) // Use this method unless you're connecting a button to a viewcontroller in storyboard
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let vc2 = segue.destinationViewController as VC2 // The viewcontroller you're going to
vc2.date = sender as NSDate // If you connected button to vc in storyboard sender will be nil, so in that case just set the vc2.date directly here
}
}
class VC2:UIViewController {
var date:NSDate! // Property you want to pass to
}