Swift: How to access second view after navigation controller? - swift

I'm really new to swift and currently working on a project for a trivia app. Right now I have it so that after pressing the start button, it pushes aside the current view controller and shows the user a new one (vc), but I'm not sure how/where to add textfields and buttons to that new view (vc) that the user sees after pressing start.
let vc = UIViewController()
vc.view.backgroundColor = .red
navigationController?.pushViewController(vc, animated: true)

You have to create another view controller with UI in it.
class SecondViewController: UIViewController {
private let textView = UITextView()
func viewDidLoad() {
super.viewDidLoad()
// Add text view with auto layout
view.addSubview(textView)
textView.translatesAutoresizingMaskIntoConstraints = false
textView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
textView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
textView.heightAnchor.constraint(equalToConstant: 44).isActive = true
}
}
Then when trying to push a new view controller use this SecondViewController
let vc = SecondViewController()
vc.view.backgroundColor = .red
navigationController?.pushViewController(vc, animated: true)

you can declare view controller with the specified identifier
let vc = UIStoryboard.init(name: "Authentication", bundle: Bundle.main).instantiateViewController(withIdentifier: "Login") as? LoginVC
self.navigationController?.pushViewController(vc!, animated: true)

Related

Presenting Modal UINavigationController Changes Background of presented UIViewController

I have a current controller within a UITabBarController
I present a UINavigationController on this UITabBarController with a modal style of .overFullScreen
I then want to present modally another controller on top of this however, the background color of the second modally presented controller is grey. Even if it's set to another color it will revert to a grey color.
How do I remove this color issue?
CODE
// MARK: - WITHIN TABBARCONTROLLER
fileprivate func presentCreatePostController() {
let controller = CreatePostController()
let navigationController = UINavigationController(rootViewController: controller)
navigationController.navigationBar.configureAppearance()
navigationController.modalPresentationStyle = .overFullScreen
navigationController.navigationBar.isTranslucent = true
selectedViewController?.present(navigationController, animated: true)
}
// MARK: - WITHIN CREATEPOSTCONTROLLER
func uploadMedia() {
print("UPLOAD IMAGE BUTTON TAPPED")
PermissionManager.shared.requestPhotoLibraryPermission()
let navigationController = UINavigationController(rootViewController: ControllerProvider.mediaPickerController)
navigationController.modalPresentationStyle = .popover
navigationController.definesPresentationContext = true
let rootViewController: UIViewController = (UIApplication.shared.windows.last?.rootViewController)!
self.view.endEditing(true)
rootViewController.present(navigationController, animated: true, completion: nil)
}

How can i present programmatically made ViewController on button click

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)

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
}

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

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)