简体   繁体   中英

TableView.delegate and segue to other VC

I wanted to have fun with some simple animations, doing To-Do-List, but well...

I have problem with segue to other VC. On the one ViewController i have TableView and button. When you press button You move (segue - push) to the other VC, but..

When the other VC is empty everything is okay, but whenever I add any textfield or something else to the second VC everything crashes, and the Error is: "fatal error: unexpectedly found nil while unwrapping an Optional value" showing on the tableView.delegate = self

Oh! Forgot about it ! Every Outlet is correct, and cell have correct ID.

What am I doing wrong?

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)
}

}

Your second VC has the wrong class assigned to it. In IB, select the second view controller and choose the 3rd tab of the Utilities panel. My guess is that you have the class set to ViewController but of course the 2nd VC should be its own class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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