【问题标题】:Passed Segue variable keeps clearing after load传递的 Segue 变量在加载后保持清除
【发布时间】:2016-06-11 00:51:31
【问题描述】:

所以我一直在努力学习 swift,但我遇到了一个让我卡了一段时间的问题。我正在使用序列将数据从第一个控制器传递到第二个控制器。

当我打印变量 onLoad 时,它会打印出传递的正确值,但是在我执行另一个函数(单击红色按钮)后,它不会打印出传递的值,而是打印出空白值。

var Level = 0;
var userId: String = "";
var clanId: String = "";
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    print(userId); //prints 97
    print(clanId); //prins 2
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
@IBAction func redButtonClick(sender: AnyObject) {
     print(userId); //prints ""
    print(clanId); //prins ""
}

正如您在代码中看到的那样,它似乎很简单,但显然我错过了它是如何工作的,因为在 onload 之后变量中不包含任何内容。

感谢大家的帮助!

------------------编辑----------------------

更改值 userId: String = "test" 更改了值 clanId: String = "test"

现在在 onload 函数中打印出来

 print(userId)
 print(clanId)

 results:
 97
 2
 test
 test

----添加-----------

@IBAction func loginButton(sender: AnyObject) {
  self.performSegueWithIdentifier("dashboard", sender: self);
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if(segue.identifier == "dashboard")
        {
            let dutydash:dashboardController  = segue.destinationViewController as! dashboardController;
            dutydash.userId = self.userId;
           dutydash.clanId = self.clanId;
        }
}

【问题讨论】:

  • 你试过不初始化userId和clanId吗? (var userId: String! var clanId: String! ) 单击按钮时它们可能会重置为原始值?
  • 进行这些更改后,我收到:97 2 致命错误:在展开可选值时意外发现 nil 如果我添加了 ?代替 !我得到输出 92, 2, nil, nil
  • 请分享一些来自第一个控制器的代码,你何时以及如何去目的地
  • @Aamir 我已将其添加到主要问题中
  • 您在该代码中设置了 userID 和 companyID,而不是 clanID

标签: ios iphone swift ios7


【解决方案1】:

以下是一个工作示例,确保您的数据设置在prepareForSegue

class prevClass: UIViewController{

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "nextView" {
            let destinationVC = segue.destinationViewController as! NextView
            destinationVC.userId = "77789";
            destinationVC.clanId = "Awesome Clan"

        }
    }
    @IBAction func next(sender: AnyObject) {
        self.performSegueWithIdentifier("nextView", sender: self)

    }
}

class NextView: UIViewController {

    var userId: String!
    var clanId: String!

    override func viewDidLoad() {
    }

    @IBAction func redButtonClick(sender: AnyObject) {
        print(userId);
        print(clanId);
    }
}

【讨论】:

    【解决方案2】:

    几个建议:

    • userIdclanId 的初始设置更改为其他设置,而不是"",例如"test",看看这是否有问题
    • 在 Swift 中,您不需要在每行末尾添加 ;
    • 所有变量都应该是驼峰式(以小写开头),所以将Level改为level

    【讨论】:

    • 我把它改成了测试,现在我得到了打印输出(在 onload 功能期间) 97 2 test test 然后当我点击其他功能时,我得到了测试
    • 好的,由于 VC 的生命周期,他们再次被分配。正如@kye 提到的,如果您确定它们将由其他 VC 设置,则可以将它们更改为 var userId: String!,否则您会崩溃。
    • 我在上面评论了他的雄蕊。它抛出一个错误,说明它在展开可选值时发现了一个 nil。
    • 您的代码中是否还有其他涉及这些变量的内容?
    【解决方案3】:

    我唯一能想到的是,您可能同时运行两个或多个视图控制器实例。在 viewdidload 和按钮操作中打印“self”并检查两者是否相等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-11
      相关资源
      最近更新 更多