简体   繁体   English

SWIFT-在viewdidload外看不到属性

[英]SWIFT - Property not seen outside viewdidload

I try to pass a string from a VC1 to VC2, but the value of tex passed into property detailItem as loaded only into viewdidload, and not in the function 我尝试将一个字符串从VC1传递到VC2,但是tex的值传递给了detailItem属性,因为它仅加载到viewdidload中,而不是在函数中加载

viewcontroller 1: ViewController 1:

@IBOutlet var tex: UITextField!
// Prepare fore segue pass the value of txt

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  if segue.identifier? == "Todo" {
    let navVC = segue.destinationViewController as UINavigationController
    let itemVC: ChecklistViewController = navVC.topViewController as ChecklistViewController
    itemVC.detailItem! = tex.text
  }
 }

viewcontroller 2: ViewController 2:

var detailItem : String!

// I can see the value of tex only in the ViewDidLoad

override func viewDidLoad() {
  super.viewDidLoad()

  println("viewDidLoad \(detailItem)")
  tableView.rowHeight = 44
}

// Here and in  the function below result nil.
required init(coder aDecoder: NSCoder) {
  println("print init \(detailItem)")
  items = [ChecklistItem]()
  super.init(coder: aDecoder)
  loadChecklistItems()
}

func documentsDirectory() -> String {
  let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as [String]
  return paths[0]
}

func dataFilePath() -> String {
  println("print path\(detailItem)")
  return documentsDirectory().stringByAppendingPathComponent("Checklists.plist")
}

func saveChecklistItems() {
  let data = NSMutableData()
  let archiver = NSKeyedArchiver(forWritingWithMutableData: data)
  archiver.encodeObject(items, forKey: "ChecklistItems")
  archiver.finishEncoding()
  data.writeToFile(dataFilePath(), atomically: true)
  println("print save\(detailItem)")
}

func loadChecklistItems() {
  let path = dataFilePath()
  if NSFileManager.defaultManager().fileExistsAtPath(path) {
    if let data = NSData(contentsOfFile: path) {
      println("print load\(detailItem)")
      let unarchiver = NSKeyedUnarchiver(forReadingWithData: data)
      items = unarchiver.decodeObjectForKey("ChecklistItems") as [ChecklistItem]
      unarchiver.finishDecoding()
    }
  }
}

Console Output: 控制台输出:

print init 
print path
print load
viewDidLoad Antwerp

Thanks Alberto 谢谢阿尔贝托

The reason is because when prepareForSegue is called your viewcontroller 2 has already been created and viewDidLoad has been called. 原因是因为调用prepareForSegue时,已经创建了viewcontroller 2并调用了viewDidLoad

Check out This Link as it has a nice writeup of the view lifecycle 查看此链接,因为它可以很好地记录视图生命周期

You will be able to see the property in all other functions that are called after viewDidLoad. 您将能够在viewDidLoad之后调用的所有其他函数中查看该属性。 Because you are setting the property after you are creating the view, so those functions are called first, when the property hasn't been set by you. 由于您是在创建视图之后设置属性的,因此,当您尚未设置属性时,将首先调用这些函数。

Try calling your functions in viewDidLoad. 尝试在viewDidLoad中调用函数。

You can't see this property in the initializer because that code is called before itemVC.detailItem! = tex.text 您无法在初始化程序中看到此属性,因为该代码是 itemVC.detailItem! = tex.text 之前 itemVC.detailItem! = tex.text itemVC.detailItem! = tex.text . itemVC.detailItem! = tex.text

You could move your call to loadChecklistItems() into viewDidLoad() . 您可以loadChecklistItems()调用移到viewDidLoad() You could also load the items as soon as the detail item is set: 您还可以在设置明细项目后立即加载这些项目:

var detailItem : String! {
    didSet {
        loadChecklistItems()
    }
}

According to the lifecycle of UIViewController, the viewDidLoad method is called exactly to let you know that the controller's view and, what's interesting for us in this case, all IBOutlets. 根据UIViewController的生命周期,将精确调用viewDidLoad方法,以使您知道控制器的视图以及在所有情况下对我们而言有趣的是所有IBOutlet。 This means that you're allowed to rely on your outlets' state only since viewDidLoad is called. 这意味着仅当调用viewDidLoad时,才允许您依赖网点的状态。 For more information please refer to "UIViewController Class Reference" at https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/ . 有关更多信息,请参阅https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/上的“ UIViewController类参考”。

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

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