简体   繁体   中英

tableview appear to be nil in ViewController when it's root by a navigation controller [SWIFT 3]

I'm having an issue with my current project. At the beginning my main focus was to create a overlay button in a TableViewController but i saw that it was more simple to use a ViewController and set on it a tableView :

class MemoList : UIViewController, UITableViewDataSource, UITableViewDelegate{

@IBOutlet weak var myTableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        myTableView.delegate = self
        #datasource is linked manually in the StoryBoard
        ...
    }
} 

When this View Controller is set as the initial controller, everything work fine but in my case, when the projet is rooted by a navigation controller, the tableview appears to be nil in the context of the 'Memolist' (when pushing on it).

Do you have any idea about this issue ? And how can I solve it ?

Thanks,

For me the problem was that I used

present(myViewController, animated: true, completion: nil)

After I tried to use segue and

performSegue(withIdentifier: "myIdentifier", sender: self)

it worked properly and tableView was no longer nil.

Why did I have this problem: When you use present() , you initialise the View Controller from code and it doesn't watch on your Storyboard . When you use segues , it uses the Storyboard and of course see all you did in your Interface Builder

You also can:

UIStoryboard(name: "StoryboardName", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerId")

This creates view controller from Storyboard, so that tableView is initialized now

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