简体   繁体   English

swift属性nil在unwindToViewController之外

[英]Swift property nil outside unwindToViewController

To pass data between views, I decided to use a "temporary" object that would act as the data model of my views. 为了在视图之间传递数据,我决定使用一个“临时”对象作为我的视图的数据模型。

var tempMedecine = TempMedecine()
var xValue = 0

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  if let dmController = segue.destinationViewController as? JRBDosageMainTableViewController {
    dmController.tempMedecine = self.tempMedecine
  }
}

 @IBAction func unwindToViewController(segue: UIStoryboardSegue) {
  if let dosageController = segue.sourceViewController as? JRBDosageMainTableViewController {
    self.tempMedecine = dosageController.tempMedecine!
    self.xValue = 10
    let dosageCell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))
    dosageCell?.detailTextLabel?.text = String(self.tempMedecine.dosageQuantity!) + " " + self.tempMedecine.dosageQuantityType!
  }
}

override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
  if identifier == "saveMedecine" {
    print(xValue)
    guard tempMedecine.name != nil else {
      Common.genericAlertController(self, title: "Error", message: "You need to define a name", preferedStyle: .Alert)
      return false
    }
    guard self.tempMedecine.dosageQuantityType != nil else {
      Common.genericAlertController(self, title: "Error", message: "You need to set a quantity", preferedStyle: .Alert)
      return false
    }

    }
    else {
      return true
    }
    return false
}

This is some of my code from the "index" viewController where I need to tackle validation. 这是我在“index”viewController中的一些代码,我需要处理验证。

As you can see all of my viewControllers have a property named tempMedecine . 正如您所看到的,我的所有viewControllers都有一个名为tempMedecine的属性。 I pass it around and update the data if needed. 我传递它并在需要时更新数据。

The problem is that self.tempMedecine.dosageQuantityType returns nil in the shouldPerformSegueWithIdentifier method but isn't returning nil in the unwindToViewController method. 问题是, self.tempMedecine.dosageQuantityType返回nilshouldPerformSegueWithIdentifier方法,但没有返回nilunwindToViewController方法。

I figured there could be two instances of my TempMedecine object, but that's not the case. 我想我的TempMedecine对象可能有两个实例,但事实并非如此。 I also thought there might be a problem with the way I pass the tempMedecine variable between my viewControllers but the property tempMedecine.name is effectively transfered, the only difference is that this property is set in the same viewController where I want to implement validation : 我还认为我在tempMedecine之间传递tempMedecine变量的方式可能有问题,但属性tempMedecine.name被有效传输,唯一的区别是这个属性设置在我要实现验证的同一个viewController中:

func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        self.tempMedecine.name = textField.text
        return true
}

It's really looking like I'm working with two different scope. 看起来我正在使用两个不同的范围。 As soon as I leave the unwindToViewController method, I would get back to another scope where the tempMedecine variable isn't updated. 一旦我离开unwindToViewController方法,我就会回到另一个未更新tempMedecine变量的范围。

But the weird part is when I use a simple variable like xValue . 但奇怪的是当我使用像xValue这样的简单变量时。 If I update its value in the unwindToViewController method I get the correct value in shouldPerformSegueWithIdentifier 如果我在unwindToViewController方法中更新它的值,我在shouldPerformSegueWithIdentifier得到正确的值

What am I missing? 我错过了什么? Thanks for your help. 谢谢你的帮助。

Okay, I fixed it. 好的,我修好了。 What happened was that I implemented some code to reset the dataSource which is tempMedecine if the user decided to click on the Back Button in the navigation bar : 发生了什么事情我实现了一些代码来重置dataSource,如果用户决定点击导航栏中的Back Button,那就是tempMedecine

if self.isMovingToParentViewController() {
  tempMedecine?.dosageQuantity = nil
  tempMedecine?.dosageQuantityType = nil
}

The thing is, I never thought the issue could come from this as I can use the tempMedecine data to set the value in my tableView after unwinding to the index viewController but I totally missed the part when object are passed my reference. 问题是,我从未想过这个问题可能来自于此,因为我可以使用tempMedecine数据在展开索引viewController后设置我的tableView中的值,但是当对象通过我的引用时我完全错过了该部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM