Performing segue after dismissing modal swift - ios

So I'm pushing a side menu onto my root menu using a modal segue and then attempting to dismiss the side menu that only covers half the page and performing the segue from the root vc when a button is pressed in the side menu. It either returns; recivere has no sague with identifier, or warning: attempt to perform segue that is not in window hierarchy.
func SubmitSettingPressed (sender: UIButton){
let mainView = MainViewController()
dismiss(animated: true) {
let vc = storyboard?.instantiateViewController(withIdentifier: "MainViewController") as? MainViewController
vc?.ToSubmition()
}
then in the root vc
func ToSubmition(){
self.performSegue(withIdentifier: "ToSubmition", sender: self)
}

Related

push to the nextViewcontroller from popUp is not working

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)

Swift Navigation bar not appearing when storyboard is referenced after login

When I launch a build for the application the login and signup work just fine but when it transfers to the HomeVC storyboard it appears empty. To make sure that it is referenced correctly I put labels on it and those were appearing during builds what just really remains invisible is the navigation bar. Pls help :)
How the HomeVC storyboard is referenced in Login and Signup
Login:
else {
let homeViewController = self.storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
self.view.window?.rootViewController = homeViewController
self.view.window?.makeKeyAndVisible()
SignUp:
func transitionToHome(){
let homeViewController = storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
view.window?.rootViewController = homeViewController
view.window?.makeKeyAndVisible()
Here is the login storyboard
The navigation bar is clearly apparent in the storyboards
You could try to use a segue
// This is used to perform the segue with the approporiate identifier
self.performSegue(withIdentifier: "Your unique identifer of the seugue", sender: self)
// Prepare for segue is used if you want to modify some properities in the desintation viewcontroller
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Your identifier of the segue" {
// This is used to fetch the destination and cast it to pass over the values
if let dc = segue.destination as? YourClassOfTheDestination {
// Here can you type into dc and pass over the values
//Example
dc.curentFruit = "Pear"
}
}
}
Here is an image on how to specify the custom identifier of the segue in the storyboard. Here is the image
If you don't want the navigation bar to be visible in certain screens you can simply use:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.isHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.isHidden = false
}
Remember to make it visible again when the view disappears.
Great question, hope the answer helps!
I believe the issue is you are making the view controller the key window and you are not adding the navigation controller. Try making the home view controller's navigation controller the key window.

navigation through view controllers while skipping one - iOS

I have a view controller as my initial view controller.
there's a button in it(GO button) which when the user taps, it should go to another view controller(let's call it Destination view controller with label 'This is where i wanna go'). Meanwhile i want to pass it through a Tabbar controller. The reason is i want to have tabbar in my navigation stack and when users presses back on Destination view controller, it must go to tabbar controller. Picture shows what i want. what can I do to skip tabbar while having it in navigation stack?
You can do that easily inside the IBAction of GO button:
#IBAction func goTapped(_ sender: UIButton) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc1 = storyboard.instantiateViewController(withIdentifier: "myTabBarViewController")
let vc2 = storyboard.instantiateViewController(withIdentifier: "myGoalViewController")
let controllers = [vc1, vc2]
self.navigationController!.setViewControllers(self.navigationController!.viewControllers + controllers, animated: true)
}
Good luck!
Going to DestinationViewController could be manually:
if let destinationViewController = self.storyboard?.instantiateViewController(withIdentifier: "Storyboard ID of DestinationViewController") {
self.navigationController?.pushViewController(destinationViewController, animated: true)
}
(Alternatively, you could make a segue from FirstViewController to the DestinationViewController directly in Storyboard)
And in your DestinationViewController, insert the TabbarController to the Navigation sequence manually after view did appear, then you are able to go back to the TabbarController:
class DestinationViewController: UIViewController {
//......
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if self.isBeingPresented || self.isMovingToParentViewController {
var viewControllers = self.navigationController?.viewControllers
if let index = viewControllers?.endIndex.advanced(by: -1),
let tabBarController = self.storyboard?.instantiateViewController(withIdentifier: "Storyboard ID of TabBarController") {
viewControllers?.insert(tabBarController, at: index)
self.navigationController?.viewControllers = viewControllers!
}
}
}
//......
}

Trouble with back button

I have strange trouble with my back button. I have 2 VC. Every VC has its own back button (they're not default, they are added as Left Bar Button Items. When I coming from VC1 (linked with Home View Controller, not shown) to VC2 I see VC2 but when I'm coming back to VC1 I see Navigation bar of VC1 and view of VC2. What Do I need to do to?
I also tried to add func but it hadn't helped.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "comeBack" {
let backVC = self.storyboard?.instantiateViewController(withIdentifier: "CollectionVC") as! RecipeCollectionViewController
self.navigationController?.pushViewController(backVC, animated: true)
}
}
use this for push VC1 TO VC2
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("VC2") as! VC2
self.navigationController?.pushViewController(vc, animated: true)
and use this for back from VC2 TO VC1
self.navigationController?.popViewControllerAnimated(true)
For pop specific view controller
let viewControllers: [UIViewController] = self.navigationController!.viewControllers
for vc in viewControllers {
if VC is YourViewController {
self.navigationController!.popToViewController(vc, animated: true)
}
}
If Pop previous controller than use
self.navigationController?.popViewControllerAnimated(true)

Swift Xcode Button not showing next view

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.

Resources