How to Push and Present to Uiviewcontroller Programmatically Without Segue in iOS Swift 3

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 send data between view controllers without using segue in IOS 9

You can instantiate the destination view controller using storyboard instance and pass the data further :

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "TestViewController") as! TestViewController
controller.variableX = self.variableX
self.present(controller, animated: true, completion: nil)

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)

How to Navigate between Viewcontrollers without any action from user swift

I think u do not require a segue for this. U can just push navigation Controller.

let storyBoardHome = UIStoryboard(name:storyBoardName, bundle: nil)

 let 1stViewController = storyBoardHome.instantiateViewController(withIdentifier: <1stViewControllerID>)


let 2ndViewController = storyBoardHome .instantiateViewController(withIdentifier: 2ndViewControllerID)


self.navigationController?.pushViewController(1stViewController/2ndViewController, animated: true)

Passing Data between View Controllers without segue

If its a small data like some string or variable you can use UserDefault or pass data using that class variable like vc2.data = data
How to use UserDefaults How to use UserDefaults in swift?

If its more like table or number of user list you can use plist store in your bundle at can retrieve from any view controller
using Plist How do I get a plist as a Dictionary in Swift?

if two controller are some how connected you can use delegates.
delegats:- Delegates in swift?

You can also use notifications if you are not sure when data will be available/ or send base on some action, or send if something is trigged.

Pass data using Notifications How to pass data using NotificationCentre in swift 3.0 and NSNotificationCenter in swift 2.0?

Programmatically Pushing a ViewController

If TabBarController comes after NavigationController then NavigationController can become nil. You should rather put TabBarController first and then put each ViewController (that are related to each tab) into there own NavigationController.

Storyboard:

Each Tab's ViewController gets their own NavigationController

Programmatically:

You need to create your TabBarController like this...

window = UIWindow(frame: UIScreen.main.bounds)
let tabCon = UITabBarController()
let navCon1 = UINavigationController(rootViewController: ViewController())
let navCon2 = UINavigationController(rootViewController: CreateJournalViewController())
let navCon3 = UINavigationController(rootViewController: AnotherViewController())
tabCon.viewControllers = [navCon1, navCon2, navCon3]
tabCon.tabBar.items?[0].title = NSLocalizedString("VC", comment: "comment")
tabCon.tabBar.items?[1].title = NSLocalizedString("CJV", comment: "comment")
tabCon.tabBar.items?[2].title = NSLocalizedString("AVC", comment: "comment")
window?.rootViewController = tabCon
window?.makeKeyAndVisible()

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 3. Can present a UIViewController with UIWebViewDelegate but not push it

The problem has nothing to do with the web view, the web view controller, or anything else except where you're pushing the view controller. You are saying:

let navController = (UIApplication.shared.delegate as! AppDelegate).navController
navController!.pushViewController(myWebViewController, animated: true)

That is highly unusual. The usual thing is that we are inside a navigation interface already, and we push onto its stack:

let navController = self.navigationController
navController!.pushViewController(myWebViewController, animated: true)

But you are not saying that. Why not? I'm guessing it's because self does not have a navigationController. So you are successfully pushing onto the navigation controller, all right, but you are not seeing anything happening because the navigation controller is behind the scenes — the self view controller's view is blocking it or has replaced it.

And that explains why you never detect the url loading. Your call to webView.loadRequest is in viewWillAppear. But this view will not appear; it is behind the scenes. viewWillAppear is never called. Similarly viewDidLoad is not called, because this view is not going to load; the navigation controller is not occupying the interface — self is.



Related Topics



Leave a reply



Submit