2014-11-05 32 views
0

調用變量我試圖創建我的第一個iOS應用斯威夫特 - 意外發現零而展開的可選值 - 從委託

我得到這個錯誤「意外發現零而展開的可選值」當我嘗試從我委託視圖控制器

我試圖讓一個壓制單元格的值訪問變量設置爲標籤文本我的目標控制器

上這是第一個控制器

class SearchStationViewController: UITableViewController, writeValueBackDelegate { 
    var selectedStation: String! 
    var fromStationValue: String! = "None" 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     selectedStation = stations[indexPath.row].StationName 
     fromStationValue = selectedStation 

     let vc = SelectedStationViewController() 
     vc.fromStationValue = self.fromStationValue 
     vc.delegate = self 

     self.dismissViewControllerAnimated(true, completion: nil) 
    } 
} 

而且,我想它的label.text目標控制器改變

protocol writeValueBackDelegate { 
    var fromStationValue: String! {get set} 
} 

class SelectedStationViewController: UITableViewController { 
    @IBOutlet weak var stationFrom: UILabel! 
    var delegate: writeValueBackDelegate! = nil 
    var fromStationValue: String! = "None" 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     stationFrom.text = fromStationValue   
    } 
} 
+2

什麼是錯誤*完全*?你的問題缺乏一些細節。我認爲你必須從故事板實例化你的視圖控制器,而不是直接和你的IBOutlet是零。 – 2014-11-05 12:11:00

+0

當我嘗試將標籤「stationFrom」設置爲從第一個視圖控制器檢索到的新值「fromStationValue」時,錯誤顯示爲「意外地發現零,同時展開可選值」。對不起,我不清楚 – StereoNight 2014-11-05 13:48:16

+0

我無法弄清楚你的視圖層次結構。當選擇電臺時,是否顯示「SearchStationViewController」,然後顯示「SelectedStationViewController」?您現在擁有它的方式是使用代理/協議將數據從'SelectedStationViewController'傳遞給'SearchStationViewController',而不是相反。 – 2014-11-05 16:00:19

回答

2

所以我就指的回答我這一天早些時候給:Flappy Bird Coding Error in Swift

你必須初始化控制器與相應的xib或故事板。例如:

let storyboard = UIStoryboard(name: "myStoryboardName", bundle: nil); 
let vc = storyboard.instantiateViewControllerWithIdentifier("myVCID") as UIViewController; 

(從Instantiate and Present a viewController in Swift

否則您的視圖控制器不會知道任何網點,他們將是零。

相關問題