I'm trying to push to different viewcontroller programatically using this code:
let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! MenuController
self.navigationController?.pushViewController(loginVC, animated: true)
Here is my setup: I have swift file called: MenuController which contains this code I set the identifier name to: LoginViewController I set the class of both ViewControllers set to MenuController. But whenever I press the button to trigger the event it doesn't do anything.
Here is the rest of my code as requested:
import UIKit
class MenuController: UIViewController {
var thingy = 0
#IBAction func ThingyBttn(_ sender: UIButton) {
if(thingy == 1) {
print("ok")
}else{
print("good")
let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! MenuController
self.navigationController?.pushViewController(loginVC, animated: true)
}
}
#IBOutlet var myButton: UIButton!
#IBAction func Button(_ sender: UIButton) {
if let ButtonImage = myButton.image(for: .normal),
let Image = UIImage(named: "ButtonAppuyer.png"),
UIImagePNGRepresentation(ButtonImage) == UIImagePNGRepresentation(Image) {
thingy = 1
print("1")
} else {
thingy = 2
print("2")
}
}
}
I know that everything else with the code works fine because it's printing the message.
You need to insert the vc you do the push from inside a navigationController as this
self.navigationController?
is nil , or do it programmatically
Edit:
Replace this
let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! MenuController
self.navigationController?.pushViewController(loginVC, animated: true)
with
let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! MenuController
let nav = UINavigationController(rootViewController:self)
(UIApplication.shared.delegate as! AppDelegate).window!.rootViewController = nav
nav.pushViewController(loginVC, animated: true)
I think your view controllers are not in Navigation Stack thats why its not pushing to next view controller
let nextVC = self.storyboard?.instantiateViewController(withIdentifier: “nextvc”) as! nextvc
let navigation = UINavigationController(rootViewController:self)
UIAppliaction.window!.rootViewController = navigation
navigation.pushViewController(nextVC, animated: true)
Related
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)
}
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)
}
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
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
}
//
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