Robin Robin - 6 months ago 108
Swift Question

pushViewController don't work when my tableView is in an TabBar

I have a TableViewController in a TabBar.
When I select one cell of my tableView, I want start a new controller with pushViewController(MyNewController).

This is my code :

In my TableView :

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myController = (storyboard.instantiateViewControllerWithIdentifier("MyViewController") as? MyViewController)!
myController.parameter = self.tableau[indexPath.row]
UINavigationController().pushViewController(myController, animated: true)
}


In my TabBar :

func viewDidLoad() {
super.viewDidLoad()
var controllerArray: [UIViewController] = []

let controller: UIViewController = ClubPreviewListViewController()
controller.title = "MES CLUBS"
controllerArray.append(controller)

self.view.addSubview(pageMenu!.view)
}


(I use CAPSPageMenu for customize my TabBar, but it's not the problem, I have the same problem without)

In my controller :

deinit {
print ("TEST")
}


When I select a cell, the log write "TEST" everytime I select but don't change the view.

I think it's my navigationController the problem, but I don't know how to fix it.

Before I implement the TabBar, I use my TableView alone, and the push did works.

Sorry for my english ! Thanks for your help.

EDIT:

I change my pushViewController :

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myController = (storyboard.instantiateViewControllerWithIdentifier("MyViewController") as? MyViewController)!
myController.parameter = self.tableau[indexPath.row]
//UINavigationController().pushViewController(myController, animated: true)
self.navigationController?.pushViewController(myController, animated: true)
}


Same reaction.

My NavigationController isn't directly link with my TabBar. I need to create an other one ? I don't really understand how NavigationController works !

This is my configuration

EDIT2:

If I use the navigationController of my TabBar and not of my TableView, the view change !

self.saveTabBarNavigationController.pushViewController(myController, animated: true)

Answer

You are creating an instance of a navigation controller on the line

UINavigationController().pushViewController(myController, animated: true)

Once this method finishes executing, the navigation controller and its view controllers will be deallocated because nothing retains your navigation controller. Causing 'TEST' to be printed in your deinit() function.

You should be obtaining the navigation controller that owns your current tab bar controller and pushing onto the stack using that. Without knowing the structure of your application it is difficult to know where your navigation controller is (if it exists).

Check your storyboard, but essentially try:

self.navigationController.pushViewController(myController, animated: true)