antggt
antggt

Reputation: 21

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,

Upvotes: 0

Views: 485

Answers (1)

ramzesenok
ramzesenok

Reputation: 6881

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

Upvotes: 1

Related Questions