19

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.

2 Answers 2

53

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
Sign up to request clarification or add additional context in comments.

3 Comments

okay, i edited my code so you can see what I did. In your solution you used the identifier of the ViewController presented in the storyboard. Is it possible to create my navigation controller without the help of the storyboard?
thank you, it worked. I had a crash because I declared the SecondViewController as a UINavigationController. After changing it back to UIViewController, it worked just fine. Could you give me an example of how to add an navigationItem to the SecondViewController? =)
Is this all in the AppDelegate file?
4
var objVC: UIViewController? = storyboard.instantiateViewController(withIdentifier: "ViewController")

var aObjNavi = UINavigationController(rootViewController: objVC)

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

3 Comments

is this objective-c syntax? Can you explain what you code does? I like to understand the meaning behind it =)
By this you can add a UINavigationController to a UIViewController. So, add this where you want a navigation controller before your view controller
Okay, but you used the storyboard, didn't you? Is there a way without the sb?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.