通过segue传递变量?Xcode 8 斯威夫特 3 [英] Passing a variable through a segue? Xcode 8 Swift 3

查看:23
本文介绍了通过segue传递变量?Xcode 8 斯威夫特 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在创建一个闹钟.一个视图控制器是一个表视图.另一个视图控制器由一个带有提交按钮的 UIDatePicker 组成.目标是当用户单击提交按钮时,它会在日期选择器上保存日期.除了保存日期外,它也是 table view controller 的 segue.我试图在单元格中显示保存为标签的时间.

So I am creating an alarm clock. One view controller is a table view. The other view controller consists of a UIDatePicker with a submit button. The goal is that when the user clicks the submit button, it saves the date on the date picker. As well as saving the date, it is a segue to the table view controller. I am trying to display the time saved as the label in the cell.

  1. 这是我的 DatePicker 视图控制器代码.我在函数之外定义了变量.然后当他们点击提交时,它应该更新值.但是当它通过 segue 时它不会更新它.

  1. This is my code for the DatePicker view controller. I defined the variable outside of the function. Then when they click submit it should update the value. However it does not update it when it passes through the segue.

 var myDate = "1:39 PM"




@IBAction func submitDate(_ sender: AnyObject) {

   myDate = DateFormatter.localizedString(from: dateOutlet.date, dateStyle: DateFormatter.Style.none, timeStyle: DateFormatter.Style.short)


}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "save" {

        let toViewController = segue.destination as! TableViewController

        toViewController.myDate = myDate



    }
}

  • 这是我的表格视图控制器代码.当我单击提交时,它不会将标签显示为 DatePicker 中选择的时间.它显示1: 39 PM".这就是我最初定义的.

  • This is my code for the table view controller. When i click submit, it does not show the label as the time chosen in the DatePicker. It shows "1: 39 PM." Which was what I initially defined it as.

    var myDate = "0"
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    
    
        let cell = Bundle.main.loadNibNamed("TableViewCell", owner: self, options: nil)?.first as! TableViewCell
    
    
        cell.textLabel?.text = myDate
    
        return cell
    
    
    
    }
    

  • 推荐答案

    不要费心在源视图控制器中声明 myDate 除非你以后要使用它.

    Don't bother trying to declare myDate in your source view controller unless you are going to use it later.

    此外,如果您的 submitDate 函数中没有任何代码来呈现您的目标视图控制器,您可能不需要它作为您的 'save'您设置的 segue 会自动处理.

    Also, if you didn't have any code in your submitDate function to present your target view controller, you probably don't need it as your 'save' segue you set takes care of that automatically.

    在源视图控制器中

    @IBAction func submitDate(_ sender: AnyObject) {
    
       //don't need anything here - remove this function unless doing anything else
    
    
    }
    
    
       override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
        if segue.identifier == "save" {
            if let toViewController = segue.destination as? TableViewController {
                toViewController.myDate = DateFormatter.localizedString(from: dateOutlet.date, dateStyle: DateFormatter.Style.none, timeStyle: DateFormatter.Style.short)
            }
        }
    }
    

    在目标视图控制器中

    var myDate: String!
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    
    
        let cell = Bundle.main.loadNibNamed("TableViewCell", owner: self, options: nil)?.first as! TableViewCell
    
    
        cell.textLabel?.text = myDate
    
        return cell
    
    
    
    }
    

    这篇关于通过segue传递变量?Xcode 8 斯威夫特 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆