Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch to other view controller programmatically in Swift 4?

Tags:

ios

swift

I need to switch page(other view controller) using a button. I have two controllers (viewController to HomeViewController) I have a button created in viewController:

let acceptButton: UIButton = {
    let button = UIButton(type: .system)
    button.setTitle("Accept", for: .normal)
    button.setTitleColor(.blue, for: .normal)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
    button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
    //button.sendAction(Selector(buttonAction), to: HomeViewController(), for: .touchUpInside)

    return button

}()

and I have set a function and used a print statement to test the function:

 @objc func buttonAction(sender: UIButton){
 print("button pressed")
}

I executed the program and when I press the button it prints out "button pressed"

Then I added the following into the function:

  let HomeViewController = ViewController(nibName: ViewController, bundle: nil)
  self.presentViewController(HomeViewController, animated: true, completion: nil)

As a result, it does not switch to other viewController(HomeViewController).

Any idea on how should I make it works? Thank you

like image 759
JK5 Avatar asked Feb 15 '18 03:02

JK5


People also ask

How do I move from one ViewController to another in Swift 4 programmatically?

Ctrl+Drag from the “View Controller” button, to somewhere in the second View Controller(HomeViewController). It can be anywhere in the main box of the second view controller.

How do I switch between view controllers?

Right-click the control or object in your current view controller. Drag the cursor to the view controller you want to present. Select the kind of segue you want from the list that Xcode provides.


2 Answers

There are couple of ways to switch to another ViewController.

  1. Navigation

  2. Present

If you have created the controllers in storyboard then you have to first get the instance of storyboard in case of multiple storyboards.

In case of multiple storyboard you need to mention the name of the storyboard.

let sampleStoryBoard : UIStoryboard = UIStoryboard(name: "UserBoard", bundle:nil)

Then get the instance of the view controller you wish to switch.

* Make sure you set the correct Identifier in ViewController.

let homeView  = sampleStoryBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController

if you wish to present the view controller:

self.present(homeView, animated: true, completion: nil)

if you wish to push the view controller:

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

if you want to Switch to another Controller which you created as Xib

let homeView = HomeViewController(nibName: "HomeViewController", bundle: nil)

If you want to push:

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

Present the Controller:

present(homeView, animated: true, completion: nil)

Other ways to Switch between Controllers.

Wiring up the Segues

Ctrl+Drag from the “View Controller” button, to somewhere in the second View Controller(HomeViewController). It can be anywhere in the main box of the second view controller. When you release, it will show you a box like the one below.

enter image description here

in this you don't need any code to switch, it will switch on click of a button.

or you can CTLR + Drag from View controller to other controller to create segue and write below code on click of button action and switch to another view controller.

performSegue(withIdentifier: "mySegueID", sender: nil)

make sure you set the correct identifier of segue.

Details of different segues

Show — When the View Controllers are in a UINavigationController, this pushes the next View Controller onto the navigation stack. This allows the user to click the back button to return to the previous screen through the back button on the top left of the Navigation Bar.

Present modally — This presents the next view controller in a modal fashion over the current View Controller. This one doesn’t need to be part of a UINavigationController or a UISplitViewController. It just shows the destination View Controller in front of the previous one. This is usually used when the user has to either Finish or Cancel .

Custom — Exactly what it sounds like. You can make your own segue style and transitions and use it here.

like image 112
ABS Avatar answered Oct 13 '22 00:10

ABS


Switch to another viewController programmatically in Swift 4 -

Navigation process

first, you need to embed your view with Navigation Controller like -

enter image description here


For Navigation programmatically you can use below code -

  • But this code work with if you working with StoryBoard in your project. First, you need to set view identifier in storyBoard

enter image description here


Push with StoryBoard

let homeView = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
self.navigationController?.pushViewController(homeView, animated: true)

Present with StoryBoard

let homeView = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
present(homeView, animated: true, completion: nil)

Push with XIB

let homeView = HomeViewController(nibName: "HomeViewController", bundle: nil)
self.navigationController?.pushViewController(homeView, animated: true)

Present with XIB

let homeView = HomeViewController(nibName: "HomeViewController", bundle: nil)
present(homeView, animated: true, completion: nil)

For avoiding any crashes you can also try this code -

guard let homeView = HomeViewController(nibName: "HomeViewController", bundle: nil) else {
   print("Controller not available")
   return
}
self.navigationController?.pushViewController(homeView, animated: true)
like image 30
Anand Nimje Avatar answered Oct 13 '22 01:10

Anand Nimje