NavigationController PushedView Controller dismisses entire NavigationController - swift - Programmatically - swift

I'm presenting a NavigationController modally like this:
let profileViewController = ProfileViewController(nibName: nil, bundle: nil)
let navigationControllerProfile = UINavigationController(rootViewController: profileViewController)
navigationControllerProfile.setNavigationBarHidden(true, animated: false)
navigationControllerProfile.modalPresentationStyle = .fullScreen
self.present(navigationControllerProfile, animated: true, completion: nil)
Whenever I push a Viewcontroller from this NavigationController:
let showCaseViewController = ShowcaseViewController()
showCaseViewController.modalPresentationStyle = .overFullScreen
self.navigationController?.pushViewController(showCaseViewController, animated: true)
The ViewController is pushed correctly but after 1 second the entire NavigationController is dismissed.

code to present view-controller with navigation
let vc = storyboard?.instantiateViewController(withIdentifier: "firstvc") as! firstvc
let navigation = UINavigationController(rootViewController: vc)
vc.modalPresentationStyle = .fullScreen
self.present(navigation, animated: true, completion: nil)
code for perform push operation inside presented view controller
let vc = storyboard?.instantiateViewController(withIdentifier: "email") as! email
self.navigationController?.pushViewController(vc!, animated: true)

Related

error instantiating a viewcontrolelr modally via code

why with this code, outlets in second view controller are unwrapped as the were nil crashing the app? they even are not appearing. issue happens if I try to access the outlets, but not if I change the view's background.
in view controller 1 button:
let vc = SecondViewController.self.createAcertainCustomAppearenceOfVC()
vc.modalPresentationStyle = .overCurrentContext
vc.modalTransitionStyle = .crossDissolve
present(vc, animated: true, completion: nil)
in second view controller
final class func createAcertainCustomAppearenceOfVC() -> SecondViewController {
let VC = SecondViewController()
VC.view.backgroundColor = .systemRed
// VC.tappedSecondOut.setTitle("push", for: .normal)
VC.tappedSecondOut.backgroundColor = .black
return VC
}
You're trying to instantiate a view controller that has outlets in a storyboard, you need to instantiate the controller from both the storyboard name and the view controller's identifier that is set in Interface Builder.
final class func createAcertainCustomAppearenceOfVC() -> SecondViewController? {
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
return storyboard.instantiateViewController(withIdentifier: "secondViewControllerIdentifier") as? SecondViewController
}

How can i return to Main.storyboard which contains a TabBarController from another Storyboard

I have two storyboards, one which contains everything the app really is, and another which contains an "onboarding"/tutorial to my app.
Once the tutorial is done, I want to navigate back to my original view controller.
This is my code to navigate to the other storyboard:
let defaults = UserDefaults.standard
if defaults.bool(forKey: "firstOpened") {
print("First VC launched")
}else{
var vc: UIViewController
let goTo = UIStoryboard(name: "Onboarding", bundle: nil).instantiateViewController(withIdentifier: "notificationStoryboard") as! FirstOnboardingViewController
self.present(goTo, animated: true, completion: nil)
}
With this, it works, except the TabBarController is not showing, and not working the way I want it to.
And this is my code to navigate back to the Main.Storyboard:
#objc func handleSecondPush() {
//registerForRemoteNotifications()
UserDefaults.standard.set(true, forKey: "firstOpened")
let goTo = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "pushToFeedVC")
self.present(goTo, animated: true, completion: nil)
//self.performSegue(withIdentifier: "goToLink", sender: nil)
}
I have also tried this in the other Storyboard, but with this the button doesn't change the view:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController
print(controller)
self.window?.rootViewController = controller
self.window?.makeKeyAndVisible()
if let tabBarVc = self.window?.rootViewController as? UITabBarController {
tabBarVc.selectedIndex = 1
}
Question in short terms: So my question is, how can I navigate back to the main.storyboard which will contain the TabBarController with a selected index of 1, from a storyboard that doesn't contain navigation controller or a TabBarController?
When you present the onbaording you should return back to the tab with with
self.dismiss(animated:true,completion:nil)
for complex presentations you can do this for easy re-back
let vc = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController
(UIApplication.shared.delegate as! AppDelegate).window!.rootViewController = vc
What is better is to
let goTo = UIStoryboard(name: "Onboarding", bundle: nil).instantiateViewController(withIdentifier: "notificationStoryboard") as! FirstOnboardingViewController
let nav = UINavigationController(rootViewController:goTo)
nav.isNavigationBarHidden = true
self.present(nav, animated: true, completion: nil)
the flow inside the onbarding should be with pushViewController
then to dismiss in the last onbaording vc
if let tab = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController as? UITabBarController {
tab.dismiss(animated:true,completion:nil)
}

Show or open or nivigate to UITabBarController form Viewcontroller like login page in swift

My request is very simple and I am new in ios developing,
I want to show UITabBarController after login successfully
#IBAction func LoginAction(_ sender: Any) {
//correct me here!
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let tabbar:UITabBarController? = (storyBoard.instantiateViewController(withIdentifier: "LiveViewController") as? UITabBarController)
navigationController?.pushViewController(tabbar!, animated: true)
}
main.storyBoard storyBoard:
This
navigationController?
is nil in
navigationController?.pushViewController(tabbar!, animated: true)
You need
let tabbar = storyboard!.instantiateViewController(withIdentifier: "LiveViewController") as! UITabBarController
let nav = UINavigationController(rootViewController: tabbar)
nav.isNavigationBarHidden = true
(UIApplication.shared.delegate as! AppDelegate).window!.rootViewController = nav

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
}
//

Dismissing the tabbar view controller in swift

I have loginViewController and I am pushing a tabBarViewcontroller which is has 3 tabs
Below's my code:
self.dismiss(animated: false, completion: nil)
var profileStoryBoard: UIStoryboard!
profileStoryBoard = UIStoryboard(name:"Login", bundle: nil)
let viewcontroller: MainViewController = profileStoryBoard.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController
self.navigationController?.pushViewController(viewcontroller, animated: false)
I then dismiss the loginViewController and push the tabBarViewController.
Now I have a sign out button on all 3 tabBar items, but it is not working.
Here's my code:
var profileStoryBoard: UIStoryboard!
profileStoryBoard = UIStoryboard(name:"Main", bundle: nil)
let viewcontroller : LoginViewController = profileStoryBoard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
self.navigationController?.pushViewController(viewcontroller, animated: false)
I am new to swift so I don't know what mistake I might be doing here.
What you are doing is pushing another controller on top of the stack
self.navigationController?.pushViewController(viewcontroller, animated: false)
You instead want to go back one label. You should use this:
navigationController?.popViewController(animated: true)