I have this custom UIViewController. It is not connected to anything storyBoard and want to present on clicking a button
class SeeListsViewController: UIViewController {
var tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
self.setUpTableView()
view.addSubview(tableView)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
// Do any additional setup after loading the view.
}
}
How can i do that. I know the storyBoard way but this one has no identifier and it is all programmatically made
The solution below if you have SeeListsViewController in the storyboard and make sure the storyboard identifier is also SeeListsViewController:
let storyboard = UIStoryboard(name: "<Your storyboard name>", bundle: nil)
let listVC = storyboard.instantiateViewController(withIdentifier: SeeListsViewController.self)
self.present(listVC, animated: true)
A programmatic way to present if you don't have a view controller in the storyboard and all the UI components should be designed programmatically in your viewcontroller
let navVC = UINavigationController(rootViewController: SeeListsViewController())
navVC.modalPresentationStyle = .fullScreen
self.present(navVC, animated: true, completion: nil)
Related
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
}
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 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)
}
The first part of my app is a Sign in section with built in Navigation controller. When the sign up section is completed and a new user is created I would like to proceed into a UITabBarController and will add seperate navigation controllers for each tab. Right now when sign up is complete and my UITabBarController is presented it still shows the UINavigationController from the first section of my app. How do I exit the UINavigationController once I enter the new UITabBarController?
And here is the code
import UIKit
class SignUpSecondViewController: UIViewController, BEMCheckBoxDelegate {
#IBAction func completeButtonAction(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "NewTabBarViewController") as! UIViewController
// Alternative way to present the new view controller
self.navigationController?.show(vc, sender: nil)
}
You should better use
self.navigationController?.present(vc, animated: true, completion: nil)
Generally there is no need for signup once main tabbar controller is up. In this case you can do this which will remove signup from memory.
let appDelegate = UIApplication.shared.delegate as! AppDelegate)
appDelegate.window?.rootViewController = vc
I'm making a login screen for my app and everything works as intented until I try to present my main view after the login(which uses a Tab Bar Controller).
The only problem is that it displays just the first item on the tab bar. I have to press the other buttons for the to appear.
Im using this code:
//after login...
var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var vc: TabBarViewController = storyboard.instantiateViewControllerWithIdentifier("MainController") as! TabBarViewController
self.presentViewController(vc, animated: true, completion: nil)
My guess is that I need to load them all at the same time, but I dont know...
This is how I did it recently. I loaded my tabBarController and the login screen together, once the user has logged in (or completed the first screen experience) you can modally dismiss the controller.
func showloginView() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loginViewController: LoginTableViewController = storyboard.instantiateViewControllerWithIdentifier("LoginTVC") as! LoginTableViewController
self.window?.makeKeyAndVisible()
var viewController = storyboard.instantiateViewControllerWithIdentifier("LoginTVC") as! LoginTableViewController
let nav = UINavigationController(rootViewController: viewController)
self.window?.rootViewController?.presentViewController(nav, animated: true, completion: nil)
}
for displaying your tabBarController and editing of any of the tabBarItems
let tabBarController = self.window?.rootViewController as? UITabBarController
let tabBarRootViewControllers: Array = tabBarController!.viewControllers!
let nav = tabBarRootViewControllers[0] as? UINavigationController
Hope this helps =)
If you link an UIButton, you can open a UITabBarController using this (providing you're using a Storyboard)
#IBAction func openTabBar(sender: AnyObject) {
var tabBarController = self.storyboard?.instantiateViewControllerWithIdentifier("tabBarController") as! UITabBarController
self.presentViewController(tabBarController, animated: false, completion: nil)
}
That'll pop the current view and open the tab bar controller.