简体   繁体   中英

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.

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
var objVC: UIViewController? = storyboard.instantiateViewController(withIdentifier: "ViewController")

var aObjNavi = UINavigationController(rootViewController: objVC)

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

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