我正在開發一個基本的鬧鐘應用程序。這是主要的故事板看起來像將自定義視圖中的值傳遞到主視圖控制器 - Swift
我添加了一個自定義視圖控制器作爲一個單獨的廈門國際銀行文件,它看起來像這樣
而且這是怎樣的接口看起來像它運行時。 (在背景主要的ViewController和在前景中CustomAlertController)
它包含一個日期選擇器。我的意思是,當用戶點擊Main故事板上的addButton時,customAlertViewController會出現,用戶可以選擇一個日期和時間作爲警報添加。當用戶點擊Custom中的AddAlertViewController時,日期和時間應該被傳遞迴數組並添加到主Storyboard視圖控制器中的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
}
代碼在報警類別
import Foundation
class Alarm{
var date : NSDate
init (date : NSDate){
self.date = date
}
}
守則CustomAlertViewController
您不必經歷整個代碼。我曾嘗試使用準備segue,但我想這不是一個可行的解決方案,當CustomAlertviewcontroller在不同的故事板(?)
我採取的下一個方法是以某種方式將日期傳遞到Alarm類的實例viewDidDisappear方法,然後將它附加到alarmDate數組(在ViewController中聲明)。
這是我陷入困境。 viewDidDisappear中的print語句將1輸出到控制檯,顯然是因爲日期已被添加。但是一旦CustomAlertViewController退出並且viewController返回,alarmDate數組將重置,並且在表視圖中不會顯示任何值。我試圖解決這個問題,但無濟於事。
我意識到如果我在故事板中使用了一個新的視圖控制器來代替單獨的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我使用雨燕2.3
完成此操作的最佳方法是使用協議和委託在視圖控制器之間傳遞信息。我會了解協議和授權,不管因爲它們是價值觀念,在你變得越來越好的時候知道。這裏有大量的資源可以通過在線和通過stackoverflow。這裏是一個教程,讓你開始:http://www.iphonelife.com/blog/31369/swift-programming-101-mastering-protocols-and-delegates-part-2 – user3353890
謝謝!我會看看 –
不客氣!我會發布代碼,但是那裏已經有很多資源,所以在這一點上它會是多餘的。如果您有具體問題,我很樂意提供幫助!另外,你可以通過通知中心完成這個工作,但這更像是一種「黑客」方式。通知中心可能會遇到類似問題。協議是我想你可以說的「正確」方式。或者如果您使用的是導航控制器,則可以使用「unwindSegue」函數來完成相同的操作。祝你好運! – user3353890