iOS- use of undeclared type viewcontroller - ios

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.

Related

Use Storyboards in Dynamic Framework

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

iOS how to start a viewcontroller from framework

I have created my private framework successfully. It has no error. I have created a project now I want a simple thing but I do not have clue. Following is what I want
I want to start the Framework view controller.
You can think it is the whole app. I want to start its first view
controller. and then the app flow is as per business logic. I
want to exit app when user exit the Framework main view controller. Actually it was the complete app but I decided to make it framework for different clients because only small modifications are needed for different clients but I do not know how it should be done.
here is how I am trying to start first view controller in my Client project but it does not recognize that controller.
#IBAction func onClickBtnStartOver(_ sender: Any)
{
let storyBoard : UIStoryboard = UIStoryboard(name: "Storyboard", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("idFrameworkVC") as FwViewController
self.presentViewController(nextViewController, animated:true, completion:nil)
}
It gives me error like "Use of undeclared type 'FwViewController'"
So what should I do to start the first viewcontroller of framework.
Note: I have viewcontrollers in one Storyboard namely Storyboard inside Framework
Declare an open func in your framework for opening first viewController :
open func presentFirstViewControllerOn(_ viewController:UIViewController) {
let storyBoard : UIStoryboard = UIStoryboard(name: "Storyboard", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("idFrameworkVC") as FwViewController
viewController.presentViewController(nextViewController, animated:true, completion:nil)
}
and call this method from client app's VC.
Deciding which is the first VC (if needed) should be the responsibility of the framework, not your client app.
use your framework bundle identifier
let frameworkBundle = Bundle(identifier: "your framework bundle identifier")
let storyboard = UIStoryboard(name: "your framework storyboard name", bundle: frameworkBundle)
let mas = storyboard.instantiateInitialViewController() as! UIViewController
self.present(mas, animated: true, completion: nil)
Try this:
UIApplication.shared.keyWindow?.rootViewController = UIStoryboard(name: "yy", bundle: nil).instantiateInitialViewController()
If everything is setup nicely then you may have not imported your framework in it. This is very important part. Make double check if You have declared Public your framework VC.
Then After it you need to start It in you method.
I will suggest to see this selected answer. And this is the case with you
PS: Do not forget to go through into comments on the selected answer as there is discussion about crash. I doubted that you face them, But if you did encounter any crash then fix the issue using that comment in answer

Switch programmatically from a viewcontroller to another

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.

Setting Init VC in App Delegate?

I am trying to init my core data stack with the init VC of my app. To do this I want to pass the core data manager i have created into the first VC upon loading.
I thought this code would make sense to pass the coredatamanager into the VC, however I get errors whichever way i write this code, im sure im missing something simple?
// Initialize Storyboard
let storyboard = UIStoryboard(name: "RoutineController", bundle: Bundle.main)
// Instantiate Initial View Controller
if let viewController = storyboard.instantiateInitialViewController() as? ViewController {
// Configure View Controller
viewController.coreDataManager = coreDataManager
// Set Root View Controller
window?.rootViewController = viewController
}
Error is simply:
Use of undeclared type 'ViewController'
However if i delete 'as? ViewController' I get an error on the following line that viewController has no property coreDataManager.
Is there some sort of delegate i need to define in the viewdidload of the view controller in sending to?
EDIT Revised my code to correct the storyboard ID, however the code inside the {} doesnt seem to execute, i get the printed error i wrote due to the if let failing, so this still isnt the right way to set the viewController...any ideas as to why?
// Initialize Storyboard
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
// Instantiate Initial View Controller
if let viewController = storyboard.instantiateInitialViewController() as? RoutineController {
// Configure View Controller
print("SENDING THIS INFO TO THE FIRST VC \(self.coreDataManager)")
viewController.coreDataManager = self.coreDataManager
// Set Root View Controller
window?.rootViewController = viewController
} else {
print("WE COULDNT SET VIEWCONTROLLER AS THE DESIGNATED VC SO MOC WASNT PASSED")
}
This is your problem:
as? ViewController
should be
as! UIViewController
alternatively this should also work:
as! RoutineController
Select the RoutineController in your storyboard and set the storyboard ID
Then use the Storyboard ID here:
if let viewController = storyboard.instantiateViewController(withIdentifier: "<storyboardID>") as? RoutineController {
The solution was that you need to define an identifier in the if let viewController to find it successfully, the working code is as follows:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let viewController = storyboard.instantiateViewController(withIdentifier: "TabBarController") as? TabBarController {
viewController.coreDataManager = self.coreDataManager
window?.rootViewController = viewController

Cannot push a view controller after successful login

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)

Resources