2016-11-04 162 views
2

我正在开发一个基本的闹钟应用程序。这是主要的故事板看起来像将自定义视图中的值传递到主视图控制器 - Swift

enter image description here

我添加了一个自定义视图控制器作为一个单独的厦门国际银行文件,它看起来像这样

enter image description here enter image description here

而且这是怎样的接口看起来像它运行时。 (在背景主要的ViewController和在前景中CustomAlertController)

enter image description here

它包含一个日期选择器。我的意思是,当用户点击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

+1

完成此操作的最佳方法是使用协议和委托在视图控制器之间传递信息。我会了解协议和授权,不管因为它们是价值观念,在你变得越来越好的时候知道。这里有大量的资源可以通过在线和通过stackoverflow。这里是一个教程,让你开始:http://www.iphonelife.com/blog/31369/swift-programming-101-mastering-protocols-and-delegates-part-2 – user3353890

+0

谢谢!我会看看 –

+1

不客气!我会发布代码,但是那里已经有很多资源,所以在这一点上它会是多余的。如果您有具体问题,我很乐意提供帮助!另外,你可以通过通知中心完成这个工作,但这更像是一种“黑客”方式。通知中心可能会遇到类似问题。协议是我想你可以说的“正确”方式。或者如果您使用的是导航控制器,则可以使用“unwindSegue”函数来完成相同的操作。祝你好运! – user3353890

回答

3

你可以使用一个协议:

protocol DateShare { 
    func share(date: NSDate) 
} 

你声明,无论你想要的任何控制范围之外。 那么你在CustomAlertVC添加

delegate: DateShare! 

财产。在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(发件人:_)范围内的这条线:

@IBOutlet func addButton(sender: UIButton?) { 
    // generate inputDate here 
    delegate.share(date: inputDate) 
} 

这应该会诀窍。

+0

感谢您的输入。我会检查并更新。 –

+0

没问题,如果你想进一步的解释我可以提供给你:) – Stormsyders

+0

我只是试过你的代码,但无法让它工作。我不明白如何让'customAlert.delegtae = self'编译。你在这里做什么意思?通过customAlert你的意思是CustomAlertViewController?我在哪里必须键入代码? –

相关问题