2

I ignore storyboard and create UINavigationController in AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()

    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    window?.rootViewController = UINavigationController(rootViewController: HomeController(collectionViewLayout: layout))//make the ViewController class to be the root

    return true
}

I have leftBarButton which switches to another UINavigationController or UICollectionViewController(according to your advice)

override func viewDidLoad() {
    super.viewDidLoad()

    let parentMenuButton = UIButton(frame: CGRect(x: 0, y: 0, width: 34, height: 34))
    parentMenuButton.addTarget(self, action: #selector(self.menuButtonOnClicked), for: .touchUpInside)
    navigationItem.leftBarButtonItem = UIBarButtonItem(customView: parentMenuButton)
}

@objc func menuButtonOnClicked(){
    print("menuButtonOnClicked button is pressed")
}

enter image description here

How can I achieve this programmatically?(switch another navigation area by pressing menu button)

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
self.navigationController!.pushViewController(secondViewController, animated: true)

Error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard () doesn't contain a view controller with identifier 'SecondViewController''

I create SecondViewController:

import UIKit

class SecondViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

}

Is there a way to do it without messing with storyboard?(only programatically)

1 Answer 1

1

You need to call push method in your menuButtonOnClicked()

 @objc func menuButtonOnClicked(){
    print("menuButtonOnClicked button is pressed")
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let secondViewController = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
    self.navigationController.pushViewController(secondViewController, animated: true)
    }

Sign up to request clarification or add additional context in comments.

4 Comments

your code not working according to xcode advice, I change your code to question
you need to set identifier of your viewController.. check the updated answer just go in your storyboard give the class and storyboard id SecondViewController
Is there a way to do it without messing with storyboard?(Updated question)
no you can't :( you need to set it in interface builder

Your Answer

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