Pass a type as variable - ios

How do I go about passing a Type as a variable in Swift 4?
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MapViewController") as! MapViewController
I have something similar to the above in a horrible if statement I'd like to move to a function, but not sure how I pass 'MapViewController' (or whatever the controller is as a variable).
'"Pseduo" Code'
function setVC(withViewController : Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MapViewController") as! withViewController
}

You could use generic parameters like here:
function setVC<T>(withViewController: T) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MapViewController") as! T
}

Related

kukushi/SideMenu Reveal Menu not working from presented anotherViewController

I use side menu library "kukushi/SideMenu"
I send data to another tableViewController (ScheduleTableVC)
let navigation = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SchedulePage") as! UINavigationController
let vc = navigation.viewControllers.first as! ScheduleTableVC
vc.text = ""
self.present(navigation, animated: true, completion: nil)
But in ScheduleTableVC library function
self.sideMenuController?.revealMenu()
not working, which must show menu
Solve it with this
let navigation = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SchedulePage") as! UINavigationController
let vc = navigation.viewControllers.first as! ScheduleTableVC
vc.text = ""
sideMenuController?.setContentViewController(to: navigation, animated: false, completion: nil)

Getting exception while push navigation controller - iOS

Why I am getting an exception while navigating. I am a beginner for the iOS development. I am navigating once the user has log in. I have used following code. I don't understand what I am missing.
I got the following exeption at last line -
Fatal error: Unexpectedly found nil while unwrapping an Optional value
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyBoard.instantiateViewController(withIdentifier: "taskListController") as! TaskListController
self.navigationController!.pushViewController(secondViewController, animated: true)
Please let me know my mistake.
You must add a navigation controller first before you push a VC
Navigation controller using code
func goToMyVC(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let mainView = storyboard.instantiateViewController(withIdentifier: "yourVC") as? yourVC
{
self.window = UIWindow(frame: UIScreen.main.bounds)
let nav1 = UINavigationController()
nav1.isNavigationBarHidden = true //Show or hide nav bar
nav1.viewControllers = [mainView]
self.window!.switchRootViewController(nav1)
self.window?.makeKeyAndVisible()
} }
In your viewcontroller call this function,
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.goToMyVC()
Navigation controller from story
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "controller") as? controller
self.navigationController?.pushViewController(vc!, animated: true)
Try the blow codes,
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "taskListController") as? TaskListController
self.navigationController?.pushViewController(secondViewController!, animated: true)
and make sure that you embedded the login view controller in a navigation controller

Storyboard returns nil on main controller after login

I am using a single Storyboard named as Main and when I am trying to redirect(push) from login screen to Home screen, On Home screen in ViewDidLoad I am getting self.storyboard is nil so UI is distorting(view, Buttons, labels are changing its position). I am getting this problem too frequent but not every-time.
Can anyone please help me out, it would be appreciated.
Try this
let startViewController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "StartViewController") as? StartViewController
self.navigationController?.pushViewController(startViewController!, animated: true)
let sb = UIStoryboard(name: "Main", bundle: nil)
let moveController = sb.instantiateViewController(withIdentifier: "StartViewController ") as! StartViewController
self.navigationController?.pushViewController(moveController, animated: true)
Please try below code
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if storyboard != nil{
let VC = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
self.navigationController?.pushViewController(VC, animated: true)
}
Hope you will get your solution if don't than tell me

iOS How to call func from another View Controller Swift

I have one problem, i want call one method from firstVC to seccondVC i try to use such code:
let storyboard: UIStoryboard = UIStoryboard.init(name: "MainScreenVC", bundle: nil)
let firstViewController: MainScreenVC = storyboard.instantiateViewController(withIdentifier: "MainScreenVC") as! MainScreenVC
firstViewController.automatingFullFill()
But immediately i received error, what is wrong?
My error:
'Could not find a storyboard named 'MainScreenVC' in bundle NSBundle
If you are taking seperate storyboard for every viewcontroller you have to give storyboard name and viewcontroller name .
let storyboard: UIStoryboard = UIStoryboard.init(name: "give storyboard name here ", bundle: nil)
let firstViewController: MainScreenVC = storyboard.instantiateViewController(withIdentifier: "viewcontrollername here") as! MainScreenVC
firstViewController.automatingFullFill()
(OR)
You can call it like this also
let mainScreenVC = UIStoryboard.init(name: "give Storyboard name here", bundle: nil).instantiateViewController(withIdentifier: "viewcontroller") as! MainScreenVC

When I push view controller its take time to navigate

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "AbcViewController") as! AbcViewController
self.navigationController?.pushViewController(vc, animated: true)
I will happy to help you
Please try this code:
DispatchQueue.main.async(execute: {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "AbcViewController") as! AbcViewController
self.navigationController?.pushViewController(vc, animated: true)
})
Thank You
Yash Shekhada

Resources