I'm trying to push a ViewController programmatically.
Code:
var plus = UIButton()
plus.addTarget(self, action: #selector(plusPressed), for: .touchUpInside)
#objc func plusPressed() {
print("plus")
let createJournalVC = CreateJournalViewController()
self.navigationController?.pushViewController(createJournalVC, animated: true)
}
What works:
Once the button is pressed, "plus" is printed to the console.
What doesn't work:
The ViewController is not pushed.
Details
I am using a Navigation Controller & Tab Bar Controller.
I am making this only programmatic, no storyboards.
There is no error printed to the console, nothing actually happens.
If TabBarController comes after NavigationController then NavigationController can become nil. You should rather put TabBarController first and then put each ViewController (that are related to each tab) into there own NavigationController.
Storyboard:
Programmatically:
You need to create your TabBarController like this...
window = UIWindow(frame: UIScreen.main.bounds)
let tabCon = UITabBarController()
let navCon1 = UINavigationController(rootViewController: ViewController())
let navCon2 = UINavigationController(rootViewController: CreateJournalViewController())
let navCon3 = UINavigationController(rootViewController: AnotherViewController())
tabCon.viewControllers = [navCon1, navCon2, navCon3]
tabCon.tabBar.items?[0].title = NSLocalizedString("VC", comment: "comment")
tabCon.tabBar.items?[1].title = NSLocalizedString("CJV", comment: "comment")
tabCon.tabBar.items?[2].title = NSLocalizedString("AVC", comment: "comment")
window?.rootViewController = tabCon
window?.makeKeyAndVisible()
It seems navigationController is nil, that's why the view controller is not pushed.
Related
So I can't use initial view controller for the tab bar controller because there is already one and this is way further along in the app. That is why I made a tab bar controller programmatically inside the first to two view controllers that I want in the tab bar controller.
Here is the VC where I add the tab bar controller to, in viewDidLoad.
let tabBarController2 = YourTabBarController()
addChild(tabBarController2)
view.addSubview(tabBarController2.view)
NSLayoutConstraint.activate([
tabBarController2.view.bottomAnchor.constraint(equalTo: mainview.bottomAnchor),
tabBarController2.view.leadingAnchor.constraint(equalTo: mainview.leadingAnchor),
tabBarController2.view.trailingAnchor.constraint(equalTo: mainview.trailingAnchor),
])
tabBarController2
.didMove(toParent: self)
below is the class of the tab bar controller
super.viewDidLoad()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let firstViewController = homepage()
let navigationController = UINavigationController(rootViewController: firstViewController)
navigationController.title = "First"
viewControllers = [navigationController]
if let secondViewController = storyboard.instantiateViewController(withIdentifier: "ranking") as? ranking {
let navgitaionController1 = UINavigationController(rootViewController: secondViewController)
navgitaionController1.title = "Second"
var array = self.viewControllers
array?.append(navgitaionController1)
self.viewControllers = array
After sucessful signin i am opening Tabbar with follwing code
let mainView = UIStoryboard(name:"Main", bundle: nil)
let tabbar = mainView.instantiateViewController(withIdentifier: "Tabbar") as? Tabbar
tabbar?.modalPresentationStyle = .fullScreen
self.present(tabbar!, animated: true, completion: nil)
Its open Tabbar with first index selected but first ViewController also cover the save area ...
and switching between the TabbarItems make it work fine ...
I am not able to understand why this happening only in one ViewController on first time open ... and how to tackle that.
Also tried following code but did't work ...
self.edgesForExtendedLayout = []
Its not the proper solution its a kind of hack ... I told in Question that its get automatically fixed after switching between TabbarItems ... so i just added Two lines right after opening Tabbar VC.
tabbar?.selectedIndex = 1
tabbar?.selectedIndex = 0
And know the complete code seems like this.
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let mainView = UIStoryboard(name:"Main", bundle: nil)
let tabbar = mainView.instantiateViewController(withIdentifier: "Tabbar") as? Tabbar
appDelegate.window = UIWindow(frame: UIScreen.main.bounds)
appDelegate.window!.rootViewController = tabbar
appDelegate.window!.makeKeyAndVisible()
tabbar?.modalPresentationStyle = .fullScreen
self.present(tabbar!, animated: true, completion: nil)
tabbar?.selectedIndex = 1
tabbar?.selectedIndex = 0
How do you change the view controller when a button is clicked. I am not using the Storyboard. The views were all created programmatically.
I tried adding a target to the button and then calling the following method to push the new View Controller:
func switchView() {
print(123)
let prodListController = ProductListController()
self.navigationController?.pushViewController(prodListController, animated: true)
}
This was called from within the UIviewController class.
Any help will be greatly appreciated. Thanks!!
EDIT:
Made the following modifications:
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = UINavigationController(rootViewController: ProductListController())
Are you using navigationController? It looks like it is "nil"...
You can do it with "show" instead:
self.show(ProductListController(), sender: self)
However, if you want to use navigationController, you have to set it up first.
For example (supposing you will start your app with a navigationController):
on didFinishLaunchingWithOptions (AppDelegate.swift):
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.main.bounds)
let initialViewController = NavViewController()
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
on viewDidLoad of NavViewController.swift:
self.pushViewController(ViewController(), animated: true)
and then, you can use your code for the button:
func switchView() {
print(123)
let prodListController = ProductListController()
self.navigationController?.pushViewController(prodListController, animated: true)
}
You can try this code to push another viewcontroller
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var navVC = UINavigationController()
var yourVC = YourViewController()
navVC.viewControllers = [yourVC]
self.window!.rootViewController = navVC
self.window?.makeKeyAndVisible()
hope this will resolve your problem.
I'm running into this issue when I'm opening a new View Controller programmatically.
let controller = self.storyboard?.instantiateViewController(withIdentifier: "overViewScreen") as! OverviewViewController
controller.user = self.userObject
let navigationController = UINavigationController(rootViewController: controller)
self.present(navigationController, animated: true, completion: nil)
The structure of my project :
storyboard
On my storyboard the tab bar is shown onto the View Controller (with the table on the right), but when I run the app it looks like this :
enter image description here
I hope you guys can help me out!
Thank you.
You are presenting NavigationController without tab bar controller. You need to present TabBarController.
Give your TabBarController identifier and instantiate them just like you've done with controller
code from comment:
let tabVC = UIStoryboard(name: "NameOfYourStoryboard", bundle: Bundle.main).instantiateInitialViewController() as! UITabBarController
let navVc = tabVC.viewControllers.first as! UINavigationController
let vc = navVc.viewControllers.first as! OverviewViewController
vc.incorrectAuthorization = SettingsAuthorizationMethod.fingerprint
vc.user = self.userObject
present(navController, animated: true, completion: nil)
Ok, I managed to fix it like this :
let vc = self.storyboard?.instantiateViewController(withIdentifier: "TabBarController") as! TabBarController
vc.user = self.userObject
let nvc = UINavigationController(rootViewController: vc)
self.present(nvc, animated: true, completion: nil)
I made a seperate controller class "TabBarController", and added a property "user" to this class. In my "OverViewController" I can get the property as follows :
let tabBar: TabBarController = tabBarController as! TabBarController
user = tabBar.user
Thanks for the help!
in my app i am using slider so i am using this library.
https://github.com/dekatotoro/SlideMenuControllerSwift
but while using this i am confused in use of navigation bar.
in my appdelegate i write down this code
window = UIWindow(frame: UIScreen.mainScreen().bounds)
storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller123 : MainViewController = (self.storyboard!.instantiateViewControllerWithIdentifier("MainViewController") as? MainViewController)!
let controllerright : drawerViewController = self.storyboard!.instantiateViewControllerWithIdentifier("drawerViewController") as! drawerViewController
let controllerleft : RightViewController = self.storyboard!.instantiateViewControllerWithIdentifier("RightViewController") as! RightViewController
let controller = SlideMenuController (mainViewController: controller123, leftMenuViewController: controllerleft, rightMenuViewController: controllerright)
navigation = UINavigationController(rootViewController: controller)
window?.rootViewController = navigation
window?.makeKeyAndVisible()
and in my mainviewcontroller i add drwer by this code
slideMenuController()?.addRightBarButtonWithImage(UIImage(named: "ic_menu_black_24dp")!)
because i want drawer on right side
and when i select one item from drawer i write this code
let controller123 : RightViewController = (self.storyboard!.instantiateViewControllerWithIdentifier("RightViewController") as? RightViewController)!
let controllerright : drawerViewController = self.storyboard!.instantiateViewControllerWithIdentifier("drawerViewController") as! drawerViewController
let controllerleft : RightViewController = self.storyboard!.instantiateViewControllerWithIdentifier("RightViewController") as! RightViewController
let controller = SlideMenuController (mainViewController: controller123, leftMenuViewController: controllerleft, rightMenuViewController: controllerright)
navigationController?.pushViewController(controller, animated: false)
but now issue is that i can not set navigation bar title or even i can not hide back button so how can i solve that?
Try something like this. Add this line in your viewDidLoad() method of drawerViewController
self.navigationItem.hidesBackButton = true
self.navigationItem.title = "Your Title"
//If you want to create a custom title View then try this
self.navigationItem.titleView = yourview
Hope this will help you.
try this code "override func viewWillAppear(animated: Bool)" in current view controller :
navigationItem.hidesBackButton = true;
navigationItem.title = "Hello";
hope this code will help you.
Try this in viewDidLoad in each ViewController that navigates from Drawer
self.navigationItem.hidesBackButton = true
self.navigationItem.title = "Title"