Alternative Ways to Push View Controllers with Storyboard Programmatically

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)

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)

How to go from one view controller in One storyboard to another viewcontroller in another storyboard programmatically?

Please follow below steps.

  1. Check your both view controllers StoryboardID (If Storyboard ID is already inserted then skip step 2).
  2. Insert View controller StoryboardID

Please check sample image of StoryboardID

Sample Image

Sample Code

   UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:"YourStoryBoardName" bundle:nil];
UIViewController *loginViewController = [storyBoard instantiateViewControllerWithIdentifier:"LoginViewController"];
// If Login View Controller is not a Navigation Controller then you need to create Navigation Controller
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:loginViewController];
[self.navigationController pushViewController:viewController animated:true];

How to push and present to UIViewController programmatically without segue in iOS Swift 3

Push

do like

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as NewsDetailsViewController
vc.newsObj = newsObj
navigationController?.pushViewController(vc,
animated: true)

or safer

  if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsDetailsVCID") as? NewsDetailsViewController {
viewController.newsObj = newsObj
if let navigator = navigationController {
navigator.pushViewController(viewController, animated: true)
}
}

present

   let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as! NewsDetailsViewController
vc.newsObj = newsObj
present(vc!, animated: true, completion: nil)

or safer

   if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsDetailsVCID") as? NewsDetailsViewController
{

vc.newsObj = newsObj
present(vc, animated: true, completion: nil)
}

How to nest a view controller in Storyboard

The closest thing in UIKit would probably be a Container View Controller:

From Android Developer Docs (fragment):

You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).

From IOS Developer Docs (Container View Controller):

Container view controllers promote better encapsulation by separating out your content from how you display that content onscreen. Unlike a content view controller that displays your app’s data, a container view controller displays other view controllers, arranging them onscreen and handling navigation between them.

Container View Controller

Step 1

Drag the UIContainerView from the component selection library as you would a normal view. Xcode will create a new ViewController for it.

Sample Image

Step 2

Add normal view constraints to the Container View, the same as any view component.

Step 3

Use the Container View Controller as an independent container.

Sample Image

I made a demo project for you: https://github.com/atapp/ContainerDEMO

If I present a ViewController programmatically without using a Navigation Controller, does the new VC replace the old one, or does it stack on top?

Here is a very simple, basic example...

The controllers are setup in Storyboard, each with a single button, connected to the corresponding @IBAction.

The DOBViewController has its Storyboard ID set to "dobVC".

The ThankYouViewController has its Storyboard ID set to "tyVC".

MainVC is embedded in a navigation controller (in Storyboard) and the navigation controller is set to Initial View Controller:

class MainVC: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
navigationController?.setNavigationBarHidden(true, animated: false)
}

@IBAction func pushToDOB(_ sender: Any) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "dobVC") as? DOBViewController {
navigationController?.pushViewController(vc, animated: true)
}
}

}

class DOBViewController: UIViewController {

@IBAction func pushToTY(_ sender: Any) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "tyVC") as? ThankYouViewController {
navigationController?.pushViewController(vc, animated: true)
}
}

}

class ThankYouViewController: UIViewController {

@IBAction func popToRoot(_ sender: Any) {
navigationController?.popToRootViewController(animated: true)
}

}

How to move from a view controller into other view controller in storyboard programmatically

Drag line from first view controller to second and set the identifier for segue:

Sample Image

In the .m file:

if([username.text isEqualToString:@"adnan"] && [password.text isEqualToString:@"bhatti123"] )
{
[self performSegueWithIdentifier:@"nextView" sender:sender];
}
else
{

}


Related Topics



Leave a reply



Submit