2016-11-09 116 views
1

我想有樂趣一些簡單的動畫,做給-do列表,但很齊全TableView.delegate與Segue公司的其他VC

我有問題,原因請看其他VC。 在一個ViewController我有TableView和按鈕。當你按下按鈕你移動(segue - push)到另一個VC,但是..當你使用另一個VC時,一切都沒問題,但是當我向第二個VC添加任何文本字段或其他東西時,一切都崩潰了,錯誤是:「致命錯誤:意外發現零而展開的可選值」顯示在tableView.delegate =自

哦!忘了它!每個出口都是正確的,並且單元格具有正確的ID。

我在做什麼錯?

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

@IBOutlet weak var tableView: UITableView! 

let items: [String] = ["test1", "test2","tes3"] 

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.dataSource = self 
    tableView.delegate = self 
    print(tableView) 

} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
    cell.textLabel?.text = items[indexPath.row] 
    return cell 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return items.count 
} 

func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

@IBAction func buttonPressed(_ sender: AnyObject) { 
    performSegue(withIdentifier: "secondVC", sender: sender) 
} 

}

+1

你檢查了'tableView'是否爲零? – Frankie

+0

好了,所以,這是它的外觀http://imgur.com/a/A8kP6 而這正是發生在你按下按鈕 http://imgur.com/a/DANMg – Magnifique

+0

您能不能告訴回調按鈕的「touchUpInside」動作的功能?同樣的問題與'準備(for segue :)'函數 – Dean

回答

0

你的第二個VC分配有錯誤的類。在IB中,選擇第二個視圖控制器並選擇Utilities面板的第三個選項卡。我的猜測是你的課程設置爲ViewController,但當然第二個VC應該是它自己的課程。

+0

第二個VC的類設置爲customClass,它是「AddTaskVC」.. – Magnifique