correct initialization navigation controller swift - swift

I have a viewcontroller, i named it as SelectionPopUpViewController. I embed that controller on a UINavigationController.
From SelectionPopUpViewController.swift, I put:
#IBAction func okTapped(sender: AnyObject) {
self.dismissViewControllerAnimated(false, completion: nil)
let homeVc = UIStoryboard(name: "Home", bundle: nil).instantiateViewControllerWithIdentifier("Home") as HomeViewController
self.navController = UINavigationController(rootViewController: homeVc)
self.window?.rootViewController = self.navController
//self.navController.pushViewController(homeVc, animated: true)
}
on above I put :
weak var window : UIWindow?
var navController : UINavigationController!
What I want is when user click that okTapped button, it will go to HomeViewController. I try to initialize like that but it did not work. Maybe my initialization wrong.
What is the correct code to made that button work (go to HomeViewController)?

Related

Swift NavigationController push method request doesn't work from PopUp view

The new PopUp ViewController is presented .overCurrentContext
There are 2 buttons with navigation for 2 other views from PopUp view.
A simple action such as print goes well, but when I try to segue programmatically (from xib file, popUpViewController) nothing happens.
let vc = RegisterEmailViewController.instantiate()
vc.coordinator = self
navigationController.pushViewController(vc, animated: false)
Goes well from any other view.
What could be wrong?
PopUpViewController code:
// Created by ᴀʟᴇxᴀɴᴅʀ ᴢʜᴇʟɪᴇᴢɴɪᴀᴋ on 23.12.2019.
// Copyright © 2019 ᴀʟᴇxᴀɴᴅʀ ᴢʜᴇʟɪᴇᴢɴɪᴀᴋ. All rights reserved.
import UIKit
class ViewController: UIViewController, Storyboarded {
weak var coordinator: MyCoordinator?
#IBAction func dismissPopUp(_ sender: Any) {
dismiss(animated: false, completion: nil)
}
#IBAction func buttonEmail(_ sender: Any) {
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "EmailViewController") as? EmailViewController
self.navigationController?.pushViewController(vc!, animated: true)
}
#IBAction func buttonPhone(_ sender: Any) {
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "PhoneViewController") as? PhoneViewController
self.navigationController?.pushViewController(vc!, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
There is MyCoordinator which inits all navigations and doesn't let me simply to segue/navigate by my own.
var childCoordinators = [Coordinator]()
var navigationController: UINavigationController
init(navigationController: UINavigationController) {
self.navigationController = navigationController
}
Therefore, a reference to the controller solved the issue. It is possible to navigate aside, until new subViews, separate views are called.
Code that worked in this case:
let vc = RegisterEmailViewController.instantiate()
vc.coordinator = self
navigationController.pushViewController(vc, animated: false)
Conclusion. Simple there are two ways:
To delete all dependencies and other methods which use
UINavigationController
To declare only by new methods for global usage.

Navigate from a controller to another controller having a root controller between them

I have 3 controllers: RootController, FirstController and SecondController.
I want to navigate from RootController -> FirstController (here I press a button) -> Take me to SecondController.
How can I do this without Notifications and Observers and without to create a segue from that button to SecondVC because after I need to press < Back and again < Back.
This is what I've tried to do but is not working:
import UIKit
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func goToSecondVC(_ sender: UIButton) {
// Go from first VC to second VC
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let showSecondVC = storyBoard.instantiateViewController(withIdentifier: "goSecondVC") as! SecondViewController
self.present(showSecondVC, animated:true, completion:nil)
}
}
For your code example to work your SecondViewController needs to have the Storyboard ID set to "goSecondVC". This is done in the Identity inspector in the third tab in the right pane. This will present your ViewController modally. However since you have a navigationController you most likely want to push the SecondViewController on the navigationStack like so:
navigationController?.pushViewController(secondVC, animated: true)

xcode swift4 how do I change the page in viewdidload

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

Push navigation from xib Files to storyboard viewcontroller in swift3 [duplicate]

I have a view in my xib file which contain buttons. i want to move to a ViewController when i will press the button (#IBAction). I have used below code
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("About") as! AboutViewController
self.presentViewController(nextViewController, animated: true, completion: nil)
I am getting the error "Value of type 'SlideMenuView' has no member 'presentViewController'.
because my class is a UIView type :
class SlideMenuView: UIView {
}
so how can I navigate to other view controller.
That is beacuase the class you are trying to present from is a UIView and not a UIViewController. It has no Present method.
I'm guessing your view (SlideMenuView) is embedded inside a viewcontroller. what you need to do is implement a delegate, and inform your containing viewController to present next Viewcontroller.
code below:
#protocol SlideMenuViewDelegate: class {
func slideMenuViewAboutButtonClicked(menuView: SlideMenuView)
class SlideMenuView: UIView {
weak var delegate: SlideMenuViewDelegate?
#IBAction func aboutButtonClicked(sender: AnyObject) {
self.delegate?.slideMenuViewAboutButtonClicked(self)
}
now, in your viewController, implement this delegate method:
func slideMenuViewAboutButtonClicked(menuView: SlideMenuView) {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("About") as! AboutViewController
self.presentViewController(nextViewController, animated: true, completion: nil)
}
Also, dont forget to assign the sliderMenuView object the viewcontroller as a delegate.
something like:
self.sliderMenuView.delegate = self // (self == the containing viewController
I did it in a different way. In class file
class SlideMenuView: UIView {
var navigationController: UINavigationController? // Declare a navigation controller variable
// And create a method which take a navigation controller
func prepareScreen(navController: UINavigationController)-> UIView {
navigationController = navController
let nibView = NSBundle.mainBundle().loadNibNamed("SlideMenuView", owner: self, options: nil)[0] as! UIView
self.addSubview(nibView)
return nibView
}
// In Button action
#IBAction func btnAction(sender: UIButton) {
var storyBoard = UIStoryboard(name: "Main", bundle: nil)
let nextViewController = storyBoard!.instantiateViewControllerWithIdentifier("NextViewController") as! UIViewController
navigationController?.pushViewController(nextViewController, animated: true)
}
}
// For calling from UIViewController
slideBarMenuIstance.prepareScreen(self.navigationController!)

How to correctly display a tab controller in swift

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.