I'm translating an old app made in Objective-C to the new SWIFT 2.0 language, but I'm having some hard time in this particular line of code:
UIStoryboard *sdkStoryboard = [UIStoryboard storyboardWithName:#"MyStoryboard" bundle:[NSBundle bundleWithIdentifier:#"thirdParty.MySDK"]];
The problem lies in the "bundle" part, I made many researches but none of them could do the trick, as most of the examples found were to "bundle: nil"
This is what I could do by now:
let sdkStoryboard: UIStoryboard = UIStoryboard(name: "MyStoryboard", bundle: ???)
Thanks!
How about:
UIStoryboard(name: "MyStoryboard", bundle: NSBundle(identifier: "thirdParty.MySDK"))
let storyboard = UIStoryboard(name: "MyStoryboard", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("thirdParty.MySDK") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
Try this:
let Bundlepath = NSBundle(identifier: "thirdParty.MySDK")
let sdkStoryboard: UIStoryboard = UIStoryboard(name: "MyStoryboard", bundle: Bundlepath )
Regards!
Related
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
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
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
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
}
In the scenario where you have a viewController which you want to present as root view above everything else what is the right way to do this?
let Storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let MY_VIEW = Storyboard.instantiateViewControllerWithIdentifier("VIEWID")
//Is this the right way?
UIApplication.sharedApplication().delegate.window?.rootViewController?.presentViewController(MY_VIEW, animated: true, completion: nil)
//Or this?
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(MY_VIEW, animated: true, completion: nil)
In other words why would you use UIApplication.sharedApplication().delegate.window? over UIApplication.sharedApplication().keyWindow?.rootViewController? and in what scenarios? What would be the pros/cons of using one or the other?
You can do as follow.
let rootController = storyboard.instantiateViewControllerWithIdentifier("VIEWID") as! SplashVC
if self.window != nil {
self.window!.rootViewController = rootController
}
advantage of this is not getting crash when window is nil.
Another way is that(Most secure way i think)
let navigationController:UINavigationController = storyboard.instantiateInitialViewController() as! UINavigationController
let rootViewController:UIViewController = storyboard.instantiateViewControllerWithIdentifier("VIEWID") as! LoginVC
navigationController.viewControllers = [rootViewController]
self.window?.rootViewController = navigationController
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController: MainViewController = storyboard.instantiateViewControllerWithIdentifier("MainViewController") as! MainViewController
let rootViewController = self.window!.rootViewController as! UINavigationController
rootViewController.pushViewController(viewController, animated: true)