I have a UIViewcontroller that presents a popUpView with two buttons, cancel and continue respectively.. but the problem is that when I press the continue button, UINavigationController is not pushing to the next UIViewcontroller.. .
-- onNextButtonTapped:
#IBAction func onOkayButtonClicked(_ sender: UIButton) {
self.dismiss(animated: true, completion: {
let nextVC = self.storyboard?.instantiateViewController(withIdentifier: "nextVC") as! NextViewController
// let nextVCNav = UINavigationController(rootViewController: nextVC)
//self.navigationController?.pushViewController(nextVCNav, animated: true)
self.navigationController?.pushViewController(nextVC, animated: true)
})
}
Did miss something?
This seems like a controller chain issue. As from what I understand you must have presented the popup modally and by doing that you have moved out of the UINavigationController stack. So pushing a view from popupview controller is not an option. You can:
Create a protocol, (or block implementation) that inform viewcontroller which presented popup, that particular button was clicked (something similar to UIAlertController)
Make a popup as a view inside view controller and show and hide that with animation. That was you can add the action of the button of this popup to your view controller.
I think the UINavigationController is not in hierarchy.
Try this
let nav = UINavigationController(rootViewController: self)
UIApplication.shared.keyWindow?.rootViewController = nav
nav.pushViewController(nextVC, animated: true)
Related
When the function is done executing I want to go back to my ProfileTabViewController. I want to get sent back to the view controller keeping the ProfileTabViewControllers same navigation bar and UITabBarController from before. The way I have it now it gets sent to the ProfileTavViewController but I lose my navigation bar as well as the tab bar. How do I just send it back to the original ProfileTabViewController. The first image is the original ViewController and the second image is when it gets sent back.
#IBAction func updateAction(_ sender: Any) {
let newInterests = options.joined(separator: " , ")
if newInterests == ""{
showAlert(message: "Please select at least one interest.")
}else{
updateFunction()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ProfileTabViewController") as! UIViewController
self.navigationController?.present(vc, animated: true, completion: nil)
}
}
While Presenting a ViewController it will not show navigationbar beacause presentation depends on UIViewController not NavigationViewcontroller.
You have to use popViewController method to get navigation bar.
for controller in self.navigationController?.viewControllers {
if controller is ProfileViewController {
self.navigationController!.popToViewController(controller, animated: true)
break
}
}
//Use Pop
I have a few view controllers with navigation bar and tab bar. Then I have an initial view controller with no navigation bar and tab bar. I want to go back to the initial view controller with no other view controllers in stack. Here's what I do:
// Head back to Initial View Controller.
let initialViewController = self.storyboard!.instantiateViewController(withIdentifier: "Initial")
UIApplication.shared.keyWindow?.rootViewController = initialViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = initialViewController
This works perfectly. But then it doesn't move to another view controller from the initial view controller again. For example:
#IBAction func loginButton(_ sender: Any) {
print("Login Button Tapped.")
let storyboard = UIStoryboard(name: "SignInViewController", bundle: nil)
let signInViewController = storyboard.instantiateViewController(withIdentifier: "SignInViewController") as! SignInViewController
self.navigationController?.show(signInViewController, sender: nil)
}
Here, the print statement is run, but self.navigationController.show won't. How do all this work? And what's the right approach to accomplish this?
EDIT:
To avoid any confusion, here's my storyboard. The left most viewcontroller (pink screen) is the initial view controller I want to go back to. The right most on the same level is the view where I want to go back from.
It doesn't move to another view controller, because you are setting a simple view controller in your rootViewController.
What you need is:
let initialViewController = self.storyboard!.instantiateViewController(withIdentifier: "Initial")
let navController = UINavigationController.init(rootViewController: initialViewController)
UIApplication.shared.keyWindow?.rootViewController?.present(navController, animated: true, completion: nil)
This will create a navigationviewcontroller, so when on "Initial" you should be able to perform push, present actions of another view controllers.
From your initial view controller, you need to instantiate your navigation stack and present it modally. Then, when you want to get back to your pre-navigation initial VC, just call dismiss(animated:,completion:), which will dismiss your entire navigation stack and return you to the initial view controller.
self.navigationController.popToRootViewController(animated:) is what you're looking for.
https://developer.apple.com/documentation/uikit/uinavigationcontroller/1621855-poptorootviewcontroller
EDIT
Try dismissing the navigationController after calling popToRoot.
self.navigationController.dismiss(animated: true, completion: nil)
EDIT
Try the following:
#IBAction func loginButton(_ sender: Any) {
print("Login Button Tapped.")
let storyboard = UIStoryboard(name: "SignInViewController", bundle: nil)
let signInViewController = storyboard.instantiateViewController(withIdentifier: "SignInViewController") as! SignInViewController
present(signInViewController, animated: true, completion: nil)
//self.navigationController?.show(signInViewController, sender: nil)
}
Then to dismiss the signInViewController call self?.navigationController.dismiss(animated:true, completion:nil) from inside the signInViewController.
I am building a flow like this
FirstViewController -> SecondViewController - > Tab Bar View Controller (consists of 1. ThirdViewController and 2. FourthVIewController)
I am opening Tab Bar View Controller as a pop up form the SecondViewController. However, when I run (self.dismiss(animated: true, completion: nil)) on click of a button in ThirdViewController, it goes back to the FirstViewController.
I want to go back to the SecondViewController
Adding code.
This is how I open the tab bar view controller from my SecondViewController
let popupVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "tabBarVC") as! UITabBarController
self.addChildViewController(popupVC)
popupVC.view.frame = self.view.frame
self.view.addSubview(popupVC.view)
popupVC.didMove(toParentViewController: self)
And this is how I try to close the tab bar view controller form Third View Controller
self.dismiss(animated: true, completion: nil))
You added tabBarController in secondViewController as subView. So you need to remove that tabBarController view from super view.
For that, you need a tabBarController object.
self.tabBarController?.view.removeFromSuperview()
You can use a navigation controller in your flow, so when you are in ThirdViewController use this:
if let vc = self.storyboard?.instantiateViewController(withIdentifier: "second") as? SecondViewController {
self.navigationController?.popToViewController(vc, animated: true)
}
The below code was able to remove the tabview and take me back to the SecondViewController
self.tabBarController?.view.removeFromSuperview()
Switching to other item on UITabBar, where 0 insert index of UTTabBar item:
self.tabBarController?.selectedIndex = 0
I'm trying to code a button that simply switches to the next view. So I embedded the first ViewController into a navigation controller then I dragged in another view controller, created a new class name ViewController2 and associated that class with the second ViewController and came the second view controller a storyboard id:"view2". Then in the Button's function I added this code:
#IBAction func newGamePressed(sender: AnyObject) {
let view2 = self.storyboard?.instantiateViewControllerWithIdentifier("view2") as
ViewController2
self.navigationController?.pushViewController(view2, animated: true)
}
However when i click on the button, literally nothing happens!!! what am i doing wrong???
#IBAction func newGamePressed(sender: AnyObject) {
let VC1 = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("view2")
let navController = UINavigationController(rootViewController: VC1)
self.presentViewController(navController, animated:true, completion: nil)
}
Just try with this code.
I'm trying to switch from one UIViewController to another using code. Right now I have,
self.presentViewController(ResultViewController(), animated: true, completion: nil)
All this is doing is taking me to a black screen instead of the ResultViewController. Any suggestions?
With Storyboard. Create a swift file (SecondViewController.swift) for the second view controller
and in the appropriate function type this:
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController
self.navigationController.pushViewController(secondViewController, animated: true)
Without storyboard
let secondViewController = ViewController(nibNameOrNil: NibName, bundleOrNil: nil)
self.presentViewController(secondViewController, animated: true, completion: nil)
You can try this one
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let resultViewController = storyBoard.instantiateViewControllerWithIdentifier("ResultView") as ResultViewController
self.presentViewController(resultViewController, animated:true, completion:nil)
Make sure you set your view controller identifier.
Swift 3
To present a controller modally
let vc = self.storyboard!.instantiateWithIdentifier("SecondViewController")
self.present(vc, animate: true, completion: nil)
To show a controller
self.show(vc, sender: self)
Try this:
let vc = ViewController(nibNameOrNil: yourNibName, bundleOrNil: nil)
self.presentViewController(vc, animated: true, completion: nil)
Hope this helps.. :)
The easiest way to do this would be to create a segue by right clicking on the view you are starting on and dragging the blue line to the result view controller the hit the show segue option. Then set the identifier of the segue to "segue". Now add this code wherever you would like in your view controller:
self.performSegueWithIdentifier("segue", sender: nil)
this will trigger the segue that will go to your resultViewController
The easiest way to switch between screens in iOS is. Add a button on the current screen. Then press control and drag the button to the target screen. Then select push. And when you will tap the button the screen will be switched to the target screen.
Make sure you are using a UINavigationViewController.
Source: ios UINavigationController Tutorial.
Shaba's answer is a good start but requires the following so that you
can control the segue from within your view controller:
override func shouldPerformSegue(withIdentifier identifier: String,
sender: Any?) -> Bool {
// your code here
// return true to execute the segue
// return false to cause the segue to gracefully fail }
Source
vc = YourViewController()
UIApplication.shared.keyWindow?.rootViewController = vc
Or
vc = YourViewController()
UIApplication.shared.keyWindow?.rootViewController = UINavigationController(rootViewController: vc) //If you want to add navigation functionality to that VC