2016-06-10 50 views
0

我有两个视图控制器一个是主ViewController在底部我把容器视图和该容器连接到另一个NewViewController,我想在主ViewController中显示。 我在ViewController中 JSON数据设置为NewViewController的标签它给我的错误致命错误:意外发现零而展开的可选值错误,而分配字符串uilabel swift

ViewController.swift

let nvc = NewViewController() 
func someFunction { 
    let data = JSON as! NSDictionary 
    let result = data["result"] as! NSArray 
    let mytitle = result.valueForKey("name") as! NSArray 
    let count = mytitle.count - 1 
    for index in 0...count { 
     let nameOfPlace = mytitle[index] as! String 
     print(nameOfPlace) //prints all the names of places 
     self.nvc.nameLabel.text = nameOfPlace //Thread 1:EXC_BAD_INSTRUCTION 
    } 

NewViewController.swift

@IBOutlet weak var nameLabel: UILabel! 
+0

可能重复[致命错误:意外地发现零,而解包可选值 - 为什么?](http://stackoverflow.com/questions/26348996/fatal-error-unexpectedly-found-nil-while-unwrapping-an -optional-value-why)(specific)&[什么是“致命错误:意外地发现零,而解包可选值”是什么意思?](http://stackoverflow.com/questions/32170456/what-does-fatal-error -un-un-pre-one-optional-value)(canonical) – Hamish

+0

另外你的viewController应该创建你的故事板的参考 –

回答

0

在显示之前,请勿设置您的UIViewController的任何IBOutlet

IBOutlet可能尚未初始化。我建议制作一个字符串属性,在初始化您的UIViewController并在viewDidLoadviewWillAppear方法中将该文本设置为您的UILabel时进行设置。

希望这有助于!

+0

我做了像Nirav Doctorwala说的但没有工作 –