将值从自定义视图传递到主视图控制器 - Swift
Passing values from a custom view to Main View Controller - Swift
我正在开发一个基本的闹钟应用程序。这是主要故事板的样子
我添加了一个自定义视图控制器作为单独的 xib 文件,如下所示
这就是界面运行时的样子。 (主要ViewController在后台,CustomAlertController在前台)
它包含一个日期选择器。我的意思是,当用户单击主故事板中的 addButton 时,customAlertViewController 将出现,用户可以选择日期和时间添加为警报。当用户点击 customAlertViewController 中的 Add 时,日期和时间应该传回数组并添加到主故事板视图控制器中的 tableView。
这是我到目前为止编写的代码:
TableView
的代码
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let adate = alarmDate[indexPath.row].date
print(adate)
cell.textLabel?.text = String(adate)
return cell
}
报警中的代码class
import Foundation
class Alarm{
var date : NSDate
init (date : NSDate){
self.date = date
}
}
CustomAlertViewController
中的代码
您不必通读整个代码。我已经尝试使用 prepare for segue,但我想当 CustomAlertviewcontroller 在不同的故事板中时,这不是一个可行的解决方案(?)
我采取的下一个方法是以某种方式将日期传递给 viewDidDisappear 方法中的 Alarm class 实例,然后将其附加到 alarmDate 数组(在 ViewController 中声明)。
这就是我卡住的地方。 viewDidDisappear 中的 print 语句将 1 输出到控制台,显然是因为已附加日期。但是一旦 CustomAlertViewController 退出并且 viewController 返回,alarmDate 数组将重置并且 table 视图中不会显示任何值。我已尝试解决此问题,但无济于事。
我意识到如果我在情节提要中使用新的视图控制器代替单独的 xib 文件,我可以轻松获得相同的结果。
class CustomAlertViewController: UIViewController {
//MARK: - Properties
@IBOutlet weak var customAlertView: UIView!
@IBOutlet weak var alarmPicker: UIDatePicker!
var destinationDate = NSDate()
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName : nibNameOrNil, bundle : nibBundleOrNil)
self.modalPresentationStyle = .OverCurrentContext
}
convenience init(){
self.init(nibName : "CustomAlertViewController", bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("NSCoding not supported")
}
//MARK: - Methods
override func viewDidLoad() {
super.viewDidLoad()
print ("View loaded")
self.customAlertView.layer.borderColor = UIColor.darkGrayColor().CGColor
self.customAlertView.layer.borderWidth = 2
self.customAlertView.layer.cornerRadius = 8
view.backgroundColor = UIColor(white: 1, alpha: 0.7)
view.opaque = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewDidDisappear(animated: Bool) {
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("table") as! ViewController
let alarm = Alarm( date : destinationDate)
vc.alarmDate.append(alarm)
// vc.alarmData.reloadData()
print(vc.alarmDate.count)
}
// MARK: - Navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationVC = segue.destinationViewController as! ViewController
let alarm = Alarm( date : destinationDate)
destinationVC.alarmDate.append(alarm)
destinationVC.alarmData.reloadData()
print(destinationVC.alarmDate.count)
}
//MARK: - Actions
@IBAction func cancelButton(sender: AnyObject) {
self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func addButton(sender: AnyObject) {
//Code to correct the time zone difference
let sourceDate = alarmPicker.date
let sourceTmeZone = NSTimeZone(abbreviation: "GMT")
let destinationTimeZone = NSTimeZone.systemTimeZone()
let sourceOffset = sourceTmeZone!.secondsFromGMTForDate(sourceDate)
let destinationOffset = destinationTimeZone.secondsFromGMTForDate(sourceDate)
let interval : Double
interval = Double(destinationOffset - sourceOffset)
destinationDate = NSDate.init(timeInterval: interval, sinceDate: sourceDate)
self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)
}
}
我缺乏使用 Swift 的经验,非常感谢任何帮助。
PS 我正在使用 Swift 2.3
您可以使用协议:
protocol DateShare {
func share(date: NSDate)
}
您可以在任何控制器范围之外的任何地方声明。
然后你添加一个
delegate: DateShare!
属性 在您的 CustomAlertVC 中。在 VC 中,当您初始化 CustomAlert 时,您添加此行:
customAlert.delegate = self
当然,您在 VC 中添加了一个符合 DateShare 协议的扩展程序:
extension ViewController: DateShare {
func share(date: NSDate) {
// do whatever you can for that the date can be displayed in table view
alarmDates.append(date) // if I understood it all ?
}
}
最后在 addButton(sender: _) 范围内添加这一行:
@IBOutlet func addButton(sender: UIButton?) {
// generate inputDate here
delegate.share(date: inputDate)
}
这应该可以解决问题。
我正在开发一个基本的闹钟应用程序。这是主要故事板的样子
我添加了一个自定义视图控制器作为单独的 xib 文件,如下所示
这就是界面运行时的样子。 (主要ViewController在后台,CustomAlertController在前台)
它包含一个日期选择器。我的意思是,当用户单击主故事板中的 addButton 时,customAlertViewController 将出现,用户可以选择日期和时间添加为警报。当用户点击 customAlertViewController 中的 Add 时,日期和时间应该传回数组并添加到主故事板视图控制器中的 tableView。
这是我到目前为止编写的代码:
TableView
的代码 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let adate = alarmDate[indexPath.row].date
print(adate)
cell.textLabel?.text = String(adate)
return cell
}
报警中的代码class
import Foundation
class Alarm{
var date : NSDate
init (date : NSDate){
self.date = date
}
}
CustomAlertViewController
中的代码您不必通读整个代码。我已经尝试使用 prepare for segue,但我想当 CustomAlertviewcontroller 在不同的故事板中时,这不是一个可行的解决方案(?)
我采取的下一个方法是以某种方式将日期传递给 viewDidDisappear 方法中的 Alarm class 实例,然后将其附加到 alarmDate 数组(在 ViewController 中声明)。
这就是我卡住的地方。 viewDidDisappear 中的 print 语句将 1 输出到控制台,显然是因为已附加日期。但是一旦 CustomAlertViewController 退出并且 viewController 返回,alarmDate 数组将重置并且 table 视图中不会显示任何值。我已尝试解决此问题,但无济于事。
我意识到如果我在情节提要中使用新的视图控制器代替单独的 xib 文件,我可以轻松获得相同的结果。
class CustomAlertViewController: UIViewController {
//MARK: - Properties
@IBOutlet weak var customAlertView: UIView!
@IBOutlet weak var alarmPicker: UIDatePicker!
var destinationDate = NSDate()
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName : nibNameOrNil, bundle : nibBundleOrNil)
self.modalPresentationStyle = .OverCurrentContext
}
convenience init(){
self.init(nibName : "CustomAlertViewController", bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("NSCoding not supported")
}
//MARK: - Methods
override func viewDidLoad() {
super.viewDidLoad()
print ("View loaded")
self.customAlertView.layer.borderColor = UIColor.darkGrayColor().CGColor
self.customAlertView.layer.borderWidth = 2
self.customAlertView.layer.cornerRadius = 8
view.backgroundColor = UIColor(white: 1, alpha: 0.7)
view.opaque = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewDidDisappear(animated: Bool) {
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("table") as! ViewController
let alarm = Alarm( date : destinationDate)
vc.alarmDate.append(alarm)
// vc.alarmData.reloadData()
print(vc.alarmDate.count)
}
// MARK: - Navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationVC = segue.destinationViewController as! ViewController
let alarm = Alarm( date : destinationDate)
destinationVC.alarmDate.append(alarm)
destinationVC.alarmData.reloadData()
print(destinationVC.alarmDate.count)
}
//MARK: - Actions
@IBAction func cancelButton(sender: AnyObject) {
self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func addButton(sender: AnyObject) {
//Code to correct the time zone difference
let sourceDate = alarmPicker.date
let sourceTmeZone = NSTimeZone(abbreviation: "GMT")
let destinationTimeZone = NSTimeZone.systemTimeZone()
let sourceOffset = sourceTmeZone!.secondsFromGMTForDate(sourceDate)
let destinationOffset = destinationTimeZone.secondsFromGMTForDate(sourceDate)
let interval : Double
interval = Double(destinationOffset - sourceOffset)
destinationDate = NSDate.init(timeInterval: interval, sinceDate: sourceDate)
self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)
}
}
我缺乏使用 Swift 的经验,非常感谢任何帮助。 PS 我正在使用 Swift 2.3
您可以使用协议:
protocol DateShare {
func share(date: NSDate)
}
您可以在任何控制器范围之外的任何地方声明。 然后你添加一个
delegate: DateShare!
属性 在您的 CustomAlertVC 中。在 VC 中,当您初始化 CustomAlert 时,您添加此行:
customAlert.delegate = self
当然,您在 VC 中添加了一个符合 DateShare 协议的扩展程序:
extension ViewController: DateShare {
func share(date: NSDate) {
// do whatever you can for that the date can be displayed in table view
alarmDates.append(date) // if I understood it all ?
}
}
最后在 addButton(sender: _) 范围内添加这一行:
@IBOutlet func addButton(sender: UIButton?) {
// generate inputDate here
delegate.share(date: inputDate)
}
这应该可以解决问题。