2017-08-01 43 views
0

我无法将数据从ViewController传递到NewVC - 另一个UIViewController。我一直呆在这里几个小时没有结果,所以这是我解决这个问题的最后手段。执行功能iOS - 无法将数据传递给不同的ViewController

For underlaying idea :

My Code For ViewController

class ViewController: UIViewController{ 
    @IBOutlet weak var name: UITextField! 
    @IBOutlet weak var lastName: UITextField! 

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

    switch segue.identifier { 

    case "seg_One"? : 

     if let source = segue.source as? ViewController 
      ,let destination = segue.destination as? NewVC { 

       destination.name.text = source.name.text //Error  
       destination.lastName.text = source.lastName.text //Error 

      } 

     default: break 

     } 

    } 

// Bunch of other code such as ViewDidLoad() 
} 

NewVC code:

class NewVC: UIViewController { 

    @IBOutlet weak var name: UILabel! 
    @IBOutlet weak var lastName: UILabel! 

    override func viewDidLoad() { 
    super.viewDidLoad() 

    }  

} 

Error is: fatal error: unexpectedly found nil while unwrapping an Optional value

+4

这是因为当'prepareForSegue()'被调用时'IBOulet'尚未加载并且为零。你必须创建公共字符串属性。 – Larme

+0

https://stackoverflow.com/questions/38357065/unexpectedly-found-nil-iboutlet-in-prepareforsegue https://stackoverflow.com/questions/29239648/prepareforsegue-not-setting-uilabel https://stackoverflow.com/questions/9576177/how-can-i-set-the-value-of-a-uilabel-following-a-segue https://stackoverflow.com/questions/25422536/swift-prepareforsegue-fatal-error-unexpectedly-发现 - 零 - 解包 - 一个 – Larme

+0

谢谢Larme。 :) – Biken

回答

0

变化如下

class NewVC: UIViewController { 

    var strName: String? 
    var strLastName: String? 

    @IBOutlet weak var name: UILabel! 
    @IBOutlet weak var lastName: UILabel! 



    override func viewDidLoad() { 
    super.viewDidLoad() 
    self.name = self.strName 
    self.lastName = self.strLastName 

    }  

} 

分配而赛格瑞如下

destination.strName = source.name.text 
destination.strLastName= source.lastName.text 
+0

非常感谢。 :) – Biken

+0

标记为接受,如果作品 – KKRocks

+0

我会做一旦它让我接受你的答案。我需要等5分钟才能打勾。 – Biken

相关问题