Segue to another Storyboard with NavigationController - swift

I'm trying to fix an onboarding/walkthrough to my app. Everything works as it should, except that when I navigate to the first page, navigationController disappears.
When I close the app and start it again, there is NavigationController/bar there.
is there something wrong in my code?
#IBAction func buttonPressed(_ sender: Any) {
let storyborad = UIStoryboard(name: "Main", bundle: nil)
let mainVC = storyborad.instantiateViewController(withIdentifier: "mainVC") as! ViewController
self.present(mainVC, animated: true, completion: nil)
}

You need to present your VC with a UINavigationController, or push a new VC onto the current navigationController.
First approach pushing mainVC to current navigationController (Probably will work better in your case):
#IBAction func buttonPressed(_ sender: Any) {
let storyborad = UIStoryboard(name: "Main", bundle: nil)
let mainVC = storyborad.instantiateViewController(withIdentifier: "mainVC") as! ViewController
self.navigationController?.pushViewController(mainVC, animated: true)
}
Second approach, presenting with initializing a navigationController:
#IBAction func buttonPressed(_ sender: Any) {
let storyborad = UIStoryboard(name: "Main", bundle: nil)
let mainVC = storyborad.instantiateViewController(withIdentifier: "mainVC") as! ViewController
self.present(UINavigationController(rootViewController: mainVC), animated: true, completion: nil)
}

Related

Swift NavigationController push method request doesn't work from PopUp view

The new PopUp ViewController is presented .overCurrentContext
There are 2 buttons with navigation for 2 other views from PopUp view.
A simple action such as print goes well, but when I try to segue programmatically (from xib file, popUpViewController) nothing happens.
let vc = RegisterEmailViewController.instantiate()
vc.coordinator = self
navigationController.pushViewController(vc, animated: false)
Goes well from any other view.
What could be wrong?
PopUpViewController code:
// Created by ᴀʟᴇxᴀɴᴅʀ ᴢʜᴇʟɪᴇᴢɴɪᴀᴋ on 23.12.2019.
// Copyright © 2019 ᴀʟᴇxᴀɴᴅʀ ᴢʜᴇʟɪᴇᴢɴɪᴀᴋ. All rights reserved.
import UIKit
class ViewController: UIViewController, Storyboarded {
weak var coordinator: MyCoordinator?
#IBAction func dismissPopUp(_ sender: Any) {
dismiss(animated: false, completion: nil)
}
#IBAction func buttonEmail(_ sender: Any) {
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "EmailViewController") as? EmailViewController
self.navigationController?.pushViewController(vc!, animated: true)
}
#IBAction func buttonPhone(_ sender: Any) {
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "PhoneViewController") as? PhoneViewController
self.navigationController?.pushViewController(vc!, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
There is MyCoordinator which inits all navigations and doesn't let me simply to segue/navigate by my own.
var childCoordinators = [Coordinator]()
var navigationController: UINavigationController
init(navigationController: UINavigationController) {
self.navigationController = navigationController
}
Therefore, a reference to the controller solved the issue. It is possible to navigate aside, until new subViews, separate views are called.
Code that worked in this case:
let vc = RegisterEmailViewController.instantiate()
vc.coordinator = self
navigationController.pushViewController(vc, animated: false)
Conclusion. Simple there are two ways:
To delete all dependencies and other methods which use
UINavigationController
To declare only by new methods for global usage.

xcode swift4 how do I change the page in viewdidload

I cannot use segue on viewDidLoad.
I already check the segue is worded on another func.
When load the view check some condition then change view this is what I want.
override func viewDidLoad(){
super.viewDidLoad()
performSegue(withIdentifier: "go" , sender: self)
}
If you want to push your view controller on view did load then just use this code.
let storyBoard : UIStoryboard = UIStoryboard(name: "your_StoryBoard", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "VC_Identifier") as! TargerViewController
self.navigationController?.pushViewController(nextViewController, animated: true)
OR
You can set the target viewcontroller as root viewcontroller by check any condition.
Hope it will helps you out.
i prefer that don't use segue use as below
let storyboard = UIStoryboard(name: "YourStoryboard", bundle: nil)
let VC = storyboard.instantiateViewController(withIdentifier: "Your Identifier") as! Your ViewController
self.present(VC, animated:true, completion:nil)
user this for redirect from One VC To Another VC
Replace viewDidLoad to viewDidAppear solved my problem
override func viewDidAppear() {
super.viewDidLoad()
performSegue(withIdentifier: "go" , sender: self)
}

Swift: Gesturing doesn't bring back the TabBar

I am trying to move back from a ViewController (simple text page) to a main ViewController with a TabBar at the bottom using gesturing.
The gesture works as I return to the original main screen but there is no TabBar.
In the simple ViewController I use this code to go back to the originating ViewController.
#objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "AboutViewControllerID")
self.present(controller, animated: true, completion: nil)
if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AboutViewControllerID") as? AboutViewController
{
present(vc, animated: true, completion: nil)
}
default:
break
}
}
}
In the ViewController with the TabBarController I have tried the following lines to rejuvinate the TabBarController but without any joy.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
tabBarController?.tabBar.isHidden = false
NSLog("TabBar true")
}
Any ideas?
First if you want to go-back don't use present as it will add the same VCS twice in the stack , you have to use unwindSegue/dismiss , or load the tabBar itself with id
self.dismiss(animated: true) {
// use this if it's not directly behind the dismissed VC
let tab = storyboard.instantiateViewController(withIdentifier: "tabBarID") as! UITabBarController
UIApplication.shared.keyWindow?.rootViewController = tab
}
//

how to load a ViewController from xib: UIView file? swift 3

OK, i have 2 problem here:
1
I try to load a ViewController from a button in the xib file.
This is my button from my xib: UIView.
#IBAction func fechaSalida(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "calendario") as! CalendarioViewController
self.presentViewController(vc, animated: true, completion: nil)
}
The problem here is the presentViewController because my xib is a UIView type, his show me this error: "(value type ViewController) has no member of presentViewController".
Why i try to use this code? is because in my CalendarioViewController i have this code when pressed a button: dismiss(animated: true, completion: nil) so this is my first problem...
2
The second problem is when i have this code in a button inside of my xib: UIView, the CalendarioViewController is load ok...but the code: dismiss(animated: true, completion: nil) is not working anymore.
Here my code of my button xib:
#IBAction func fechaSalida(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc:CalendarioViewController = storyboard.instantiateViewController(withIdentifier: "calendario") as! CalendarioViewController
vc.dateDelegate = self
self.window!.rootViewController = vc
}
So..how can load a ViewController (CalendarioViewController) and the dismiss code Keep running?
ok... i have what i need.
For this work i use UITapGestureRecognizer in View Controller which holds the view:
In override func viewDidLoad() put this code:
let fechaArriboTap = UITapGestureRecognizer(target: self, action:#selector(handleArribo))
let fechaSalidaTap = UITapGestureRecognizer(target: self, action:#selector(handleSalida))
fechaArribo.addGestureRecognizer(fechaArriboTap)
fechaSalida.addGestureRecognizer(fechaSalidaTap)
Then create the methods:
func handleArribo() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc:CalendarioViewController = storyboard.instantiateViewController(withIdentifier: "calendario") as! CalendarioViewController
vc.dateDelegate = self
present(vc, animated: true, completion: nil)
}
func handleSalida() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc:CalendarioViewController = storyboard.instantiateViewController(withIdentifier: "calendario") as! CalendarioViewController
vc.dateDelegate = self
present(vc, animated: true, completion: nil)
}
and thats is! Thanks #sasquatch

how to close a tabbar swift

I have a login view controller which on a correct login will open a tabbar controller with the following method:
self.dismissViewControllerAnimated(true, completion: { self.loadHomeScreen()})
func loadHomeScreen()
{
emailField.text = ""
passwordField.text = ""
self.presentViewController(UIStoryboard.tabbarController()!, animated: true, completion: nil)
}
private extension UIStoryboard {
class func mainStoryboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) }
class func tabbarController() -> UITabBarController? {
return mainStoryboard().instantiateViewControllerWithIdentifier("TabbarControllerID") as? UITabBarController
}
}
I think my login view controller is still in the background? I have a logout button as the rightbarbutton item on each navbar in my tabgroup. I want to be able to close the tabbar on this button press. How can I achieve this. I can't find any examples. Would I be using a pop command?
UPDATE:
#IBAction func tryLogout(sender: UIBarButtonItem) {
self.dismissViewControllerAnimated(true, completion: nil)
let storyboard = UIStoryboard(name: "main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("login") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}