I'm trying to create an app which will switch programmatically from a View Controller to another. I've already tried to do that with this code:
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "EndViwController") as! EndViewController
self.navigationController?.pushViewController(EndViewController, animated: true)
But it turns me this error:
[Cannot convert value of type 'EndViewController.Type' to expected
argument type 'UIViewController']
Also, when I try this other code:
let timeLineTableVC = EndViewController()
self.present(timeLineTableVC, animated: true, completion: nil)
It gives to me a black screen on the simulator and the EndViewController doesn't appear.
In the end, when I try this:
let storyboard = UIStoryboard(name: "EndViewController", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "EndViewController")
self.present(controller, animated: true, completion: nil)
It gives me this error:
[libc++abi.dylib: terminating with uncaught exception of type
NSException (lldb)]
Your first bit of code doesn't work because EndViewController is a type. You should replace this with secondViewController that you declared above.
The second bit of code doesn't work because you are creating an 'empty' EndViewController type and not instantiating the storyboard view .
I'm guessing the third bit of code is failing because your storyboard isn't called "EndViewController" (unless you've renamed it). Try changing this to "Main" instead.
Related
Error transferring value from one view controller to another on dismiss.
I have a value in ViewController2 that I would like to transfer to ViewController1 on dismiss. In view ViewController2 I do the following to dismiss:
func OtherFunction() {
passAndDismiss(scannedScript: stringCodeValue)
}
Note: stringCode value is indeed being passed as a String to scannedScript
func passAndDismiss(scannedScript: String) {
dismiss(animated: true, completion: {
let viewcontroller = ViewController1()
viewcontroller.Textfield1.text = scannedScript
})
}
The error occurs on line:
viewcontroller.Textfield1.text = scannedScript
The error I get is:
Unexpectedly found nil while implicitly unwrapping an Optional value
Two fatal issues:
ViewController1() is a new instance of the controller which is not the instance in the storyboard.
Solution: You need the real instance either with a segue or with instantiation from the storyboard.
Even if ViewController1() was the expected controller the outlets are not connected right after the initialization.
Solution: You have to declare a temporary property for the string in ViewController1 and assign the value to the label in viewDidLoad
Here you are initialising viewcontroller with wrong way. You should load from xib or storyboard.
In your way not initialise xib or storyboards’ ui element objects. Here textfield object nil and you are trying to access text property of that. That’s why app crashed. You should use following way:
let myViewController = MyViewController(nibName: "MyViewController", bundle: nil)
OR
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "someViewController")
If you are not using xib or storyboard then please initialise textfield object in that controller’s init method
I am trying to switch from one controller to another. But the error shows undeclared type.
I have import UIKit on the controller but that also didn't work.
let storyboard = UIStoryboard(name: "ParticularUserInfo", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "AssignedScorerViewController") as! AssignedScorerViewController
viewController.USERID = USERID
self.present(viewController, animated: true, completion: nil)
Please verify that your Storyboard name is "ParticularUserInfo"? Probably it could be "Main". Also verify that in your storyboard for that particular ViewController identifier is set as "AssignedScorerViewController".
to set Identifier, select your ViewController in storyboard and set Storyboard ID, according to my attached screenshot.
I am trying to create a dynamic framework in swift.
My Framework contains a ViewController inside a Storyboard.
I am not able to open my ViewController when a push notification comes inside my framework.
However i tried to use the below code :
let bundle = Bundle(identifier: "my_Framework_bundleID")
let frameworkStoryboard: UIStoryboard = UIStoryboard.init(name: "MyFrameworkStoryboard", bundle: bundle)
let viewController = frameworkStoryboard.instantiateViewController(withIdentifier: "MyViewController")
self.present(viewController, animated: true, completion: nil)
Doing this only executes the code inside MyViewController's viewDidLoad method but does not show the loads the UI.
I also tried this:
let bundle = Bundle(identifier: "my_Framework_bundleID")
let controller = UIViewController(nibName: "MyViewController", bundle: bundle)
present(controller, animated: true, completion: nil)
Doing this i get the following error:
Attempt to present UIViewController: 0x135124920 on MyFramework.anotherViewController: 0x133df1a40 whose view is not in the window hierarchy!
self.pushviewController() and self.show() doesn't work as well.
I am stuck in this problem for two days.
I figured out a way to make it work
In the Appdelegate of my app where i am using my framework i passed the instance of rootviewcontroller like this:
test.myFunc(withRootView: (self.window?.rootViewController)!)
Now in my Framework i created a xib and a corresponding UIView file for it.
To navigate to this UIView file i used below code in "myFunc" function.
public func myFunc(withRootView rootview: UIViewController)
let modalView = MyUIViewClass(frame: self.view.bounds)
if rootview.presentedViewController == nil {
rootview.view.addSubview(modalView)
} else {
rootview.presentedViewController?.view.addSubview(modalView)
}
I want to pass value from one UIviewController to UINavigationViewController without using segue in swift 4.
My sample code is following
let vc = storyboard?.instantiateViewController(withIdentifier: "MainMenuId") as! UINavigationViewController
vc.sendValue = String //String send to sendValue which present in UINavigationController
self.present(vc!, animated: true, completion: nil)
But when I try to send at at that time my app is crashed and get error
Could not cast value of type 'UINavigationController' to 'UIViewController'
So How do I pass value from above structure without using segue. Thx in advance ;)
You can use below code:
let vc = storyboard?.instantiateViewController(withIdentifier:"MainMenuId") as! UIViewController
let nav = UINavigationController(rootViewController: vc)
vc.sendValue = String
self.present(nav!, animated: true, completion: nil)
I am trying to send it to another view on successful login so I am just testing my logic at this stage
let viewController:UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("myMainView") as! mainController
self.presentViewController(viewController, animated: true, completion: nil)
I have given my tab bar controller the name of myMainView but again i am being presented with
Could not cast value of type 'UITabBarController' (0x1abc8c828) to 'apertureswift.mainController' (0x1000210a0).
2016-10-09 11:56:17.549751 apertureswift[544:55199] Could not cast value of type 'UITabBarController' (0x1abc8c828) to 'apertureswift.mainController' (0x1000210a0).
if you send to UITabbarController you should change mainConrtoller to UITabbarController and identifier . you must set identifier for UITabController
let viewController:UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("tabcontroller") as! UITabbarController
self.presentViewController(viewController, animated: true, completion: nil)