Swift: Navigate to New Viewcontroller Using Button

Programmatically navigate to another view controller/scene

I already found the answer

Swift 4

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "nextView") as! NextViewController
self.present(nextViewController, animated:true, completion:nil)

Swift 3

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController
self.presentViewController(nextViewController, animated:true, completion:nil)

Push new view controller when button is pressed

Got an answer!

In my SceneDelegate.swift I set:

let menuViewController = UINavigationController(rootViewController: MenuViewController())
window?.rootViewController = menuViewController

Now it works! Thanks for the comments everyone :)

Swift programmatically navigate to another view controller/scene

Swift 5

The default modal presentation style is a card. This shows the previous view controller at the top and allows the user to swipe away the presented view controller.

To retain the old style you need to modify the view controller you will be presenting like this:

newViewController.modalPresentationStyle = .fullScreen

This is the same for both programmatically created and storyboard created controllers.

Swift 3

With a programmatically created Controller

If you want to navigate to Controller created Programmatically, then do this:

let newViewController = NewViewController()
self.navigationController?.pushViewController(newViewController, animated: true)

With a StoryBoard created Controller

If you want to navigate to Controller on StoryBoard with Identifier "newViewController", then do this:

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "newViewController") as! NewViewController
self.present(newViewController, animated: true, completion: nil)

Swift: Navigate to new ViewController using button

First off as I explain in this answer, you need to drag the segue from the overall UIViewController to the next UIViewController, i.e. you shouldn't specifically connect the UIButton (or any IBOutlet for that matter) to the next UIViewController if the transition's conditional:

storyboard segue

You'll also need to assign an identifier to the segue. To do so, you can select the segue arrow then type in an identifier within the right-hand panel:

segue identifier

Then to perform the actual segue, use the performSegueWithIdentifier function within your conditional, like so:

if user != nil {
self.messageLabel.text = "You have logged in";
self.performSegueWithIdentifier("segueIdentifier", sender: self)
} else {
self.messageLabel.text = "You are not registered";
}

where "segueIdentifier" is the identifier you've assigned to your segue within the storyboard.

How to Navigate from one View Controller to another using Swift

Create a swift file (SecondViewController.swift) for the second view controller
and in the appropriate function type this:

let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
self.navigationController.pushViewController(secondViewController, animated: true)


Swift 2+

let mapViewControllerObj = self.storyboard?.instantiateViewControllerWithIdentifier("MapViewControllerIdentifier") as? MapViewController
self.navigationController?.pushViewController(mapViewControllerObj!, animated: true)

Swift 4

let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "IKDetailVC") as? IKDetailVC
self.navigationController?.pushViewController(vc!, animated: true)

Navigate from NavigationController to another NavigationController with a back button

I think you cannot push Navigation Controller inside another uinavigation controller but alternativaly you can present another nav controller.
Hope it works for you.
I have created simple demo like this.
enter image description here

class ViewController: UIViewController {
@IBOutlet weak var btnCustom: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func btnForward(_ sender: Any) {
/*Uncomment the code if you want to present programatically*/
/*if let navi2 = self.storyboard?.instantiateViewController(withIdentifier: "nav2"){
self.present(navi2, animated: true) {
}
}*/
}

}

class ViewController2: UIViewController {
@IBOutlet weak var btnCustom: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func btnBack(_ sender: Any) {
if let parentNav = self.navigationController {
parentNav.dismiss(animated: true) {

}
}
}}


Related Topics



Leave a reply



Submit