Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a navigation controller programmatically in code but not as initial view controller

I am relatively new to swift and iOS and can't find an answer or any help for my problem that works.

I want to create a navigation controller view when I click on a button in my previous view. My previous view is an on-boarding with a button on the last page. I successfully connected the button to my next view but I am not able to make the view act like a navigation controller. For example, when a navigation bar is displayed, I am not able to add navigation items to it.

Is there a way to set the new view I create by clicking my button in the first view to be the root controller for my navigation view?

A short version of my code:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let button = UIButton(frame: CGRect(x: view.frame.width / 2, y: view.frame.height / 2, width: 40, height: 40))
        button.backgroundColor = .red
        button.tintColor = .white
        button.setTitle("Test", for: .normal)
        button.addTarget(self, action: #selector(change), for: .touchUpInside)
        view.addSubview(button)
    }

    func change () {
        let otherView = SecondViewController()
        self.present(otherView, animated: true, completion: nil)
    }

}

class SecondViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .yellow
    }

}

The SecondViewController gets displayed with a navigation bar but I had problems implementing navigation items, because they weren't shown.

Does the second view controller I present have to be of type UIViewController or UINavigationController?

I am aware that there are easier ways to achieve my goal with the use of the storyboard but in my opinion I understand it better by making my own references via code instead of creating them by dragging them out of the storyboard.

Edit: I have no Objective-C background and learning Swift for about 4 weeks now.

like image 547
iOS-Trainee Avatar asked Mar 15 '17 12:03

iOS-Trainee


People also ask

How do I add a navigation bar to a table view controller?

From the outline view, make sure your Table View Controller is selected. Then go to the Editor menu, and click on the Embed In submenu, and choose Navigation Controller and voila. You have your navigation controller pointing to your tableview controller with a relationship built in. This should be the selected answer.


2 Answers

You can add UINavigationController like below :

First you have to create object of SecondViewController,

let myViewController: SecondViewController? = storyboard?.instantiateViewController(withIdentifier: "SecondViewController")

Or

let myViewController: SecondViewController? = SecondViewController(nibName: "SecondViewController", bundle: nil)

Or

let myViewController: SecondViewController? = SecondViewController()

Then add Navigation to SecondViewController

let myNavigationController = UINavigationController(rootViewController: myViewController!)
    

If you want to present then use :

self.present(myNavigationController, animated: true) { 
}

If you want to push then use :

self.navigationController?.pushViewController(myNavigationController, animated: true)

If you want to set as root controller then use :

let appDelegate: AppDelegate = (UIApplication.shared.delegate as? AppDelegate)!
appDelegate.window?.rootViewController = myNavigationController
like image 182
VRAwesome Avatar answered Oct 04 '22 16:10

VRAwesome


var objVC: UIViewController? = storyboard.instantiateViewController(withIdentifier: "ViewController")

var aObjNavi = UINavigationController(rootViewController: objVC)

Now, instead of using view controller object use navigation controller object.

like image 30
Arpit Jain Avatar answered Oct 04 '22 16:10

Arpit Jain