Hi I have to access storyboard from custom framework (LoginUIModule, LoginUIModule have storyboard LoginScreen.storyboard) in app delegate.
I removed Main storyboard from Main Interface and also removed name from Main storyboard file base name as well from .plist, but I getting an error
reason: 'Could not find a storyboard named 'LoginScreen' in bundle NSBundle
Note:- LoginUIModule is separate Module and I need to access it in my main (OneAppllbs) project which is a again separate module
The Code which I used in app delegate
import LoginUIModule
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "LoginScreen", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginUIModuleViewController") as? LoginUIModuleViewController
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
You need set Bundle to access Storyboard.
First create storyboardBundle with Bundle Identifier for framework;
let storyboardName = "LoginScreen"
let storyboardBundle = Bundle(for: LoginUIModuleViewController.self)
or referencing a class in framework:
let storyboardBundle = Bundle(identifier: "com.yourframework.id")
Then create storyboard with this bundle:
let storyboard = UIStoryboard(name: storyboardName, bundle: storyboardBundle)
Get storyboard from a framework
Set Storyboard ID for .storyboard. For example frameworkStoryboardId
[Access to Framework bundle]
let frameworkStoryboard = UIStoryboard(name: "SomeViewController", bundle: frameworkBundle)
let frameworkViewController = frameworkStoryboard.instantiateViewController(withIdentifier: "frameworkStoryboardId") as? SomeViewController
Related
I have successfully configured google sign in my iOS App using Firebase.
After a successful login, I need to make the UIViewController move to HomeViewController.
The AppDelegate class has "didSignInFor" method in which I have added the following code
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "HomeVC")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
HomeVC is a storyboard id of HomeViewController and restoration id is also same.
But still, it doesn't take me to HomeViewController.
I have referred this stack overflow posts.
Opening view controller from app delegate using swift
Please tell me where I am going wrong. I have referred this video for building the app.
https://www.youtube.com/watch?v=20Qlho0G3YQ
Here's my AppDelegate.swift file https://pastebin.com/WfzhYAKH
Try:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "HomeVC")
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
This works for me.
Please comment if you have any questions. Happy to Help!
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
I'm trying to use MFSideMenu but when I ask for the menu to open the app breaks because menuContainerViewController is empty
This is the code that creates the menu at appDelegate (didFinishLaunchingWithOptions):
let s = UIStoryboard(name: "Main", bundle: nil)
let contentView = s.instantiateInitialViewController()
let leftMenu = s.instantiateViewControllerWithIdentifier("LeftMenuViewController") as! LeftMenuViewController
let container:MFSideMenuContainerViewController = MFSideMenuContainerViewController.containerWithCenterViewController(contentView, leftMenuViewController: leftMenu, rightMenuViewController: nil)
window?.rootViewController = container
window?.makeKeyAndVisible()
this is the code responsible for opening my menu:
self.menuContainerViewController.toggleLeftSideMenuCompletion({})
what might be happening? I installed the library using CocoaPods and I'm importing it properly.
Try this one,
self.menuContainerViewController.toggleLeftSideMenuCompletion(nil)
Initially I have a hierarchy below after login
-> MyCoursesViewController
-> CourseInfo UITabBarController
If the user closes the app, then re-enters, the rootViewController will be the CourseInfo UITabBarController which is correct. However when the user needs to view a different course (exits the course), they can’t go ‘back’ to MyCoursesViewController because its no longer on the stack.
In AppDelegate:
if (inCourse) {
let storyboard : UIStoryboard = UIStoryboard(name: “Main”, bundle: nil)
let courseInfoTabController = storyboard.instantiateViewControllerWithIdentifier(“CourseInfo”) as! UITabBarController
self.window?.rootViewController = courseInfoTabController
} else {
let storyboard : UIStoryboard = UIStoryboard(name: “Main”, bundle: nil)
let myCoursesViewController = storyboard.instantiateViewControllerWithIdentifier(“MyCourses”)
self.window?.rootViewController = myCoursesViewController
}
Is there some way I can put the MyCoursesViewController as the rootViewController then automatically navigate to Course Info UITabBarController just so the MyCoursesViewController is on the hierarchy incase they hit back (exits the course)?
Alternatively is it better if the user exits the course (hit back), we delete the rootViewController somehow and replace with a new rootViewController? Another option is if we just replace the rootViewController, will the old one be freed from memory or is it still referenced somewhere?
e.g.
CourseInfo UITabBarController is currently still rootViewController but now we swap it out with a new one
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let myCoursesViewController = mainStoryBoard.instantiateViewControllerWithIdentifier(“MyCourses”) as! ViewController
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController = myCoursesViewController
In your AppDelegate you can set your hierarchy. Try with something like:
let storyboard : UIStoryboard = UIStoryboard(name: “Main”, bundle: nil)
let myCoursesViewController = storyboard.instantiateViewControllerWithIdentifier(“MyCourses”)
if isInCourse{
let courseInfoTabController = storyboard.instantiateViewControllerWithIdentifier(“CourseInfo”) as! UITabBarController
let navigationBar = UINavigationController()
navigationBar.setViewControllers([myCoursesViewController,courseInfoTabController], animated: false)
self.window?.rootViewController = navigationBar
}else{
self.window.rootViewController = myCoursesViewController
}
I created a simple Cocoa touch framework with a storyboard. In my framework i have a MainViewController.swift viewcontroller.
I created a new single view project, imported my framework and tried to load my framework viewcontroller, but i got black screen. And I dont know why.
I tried load framework with this code:
let frameworkScreen : UIViewController = MainViewController()
self.presentViewController(frameworkScreen, animated: true, completion: nil)
You need to load the view controller by instantiating it from the storyboard in the framework.
Here's how. First some initial conditions:
Let's say your framework is called Coolness.
Let's say your framework's storyboard is called CoolnessStoryboard.storyboard.
Let's say your framework has a public class called CoolnessViewController.
Let's say that CoolnessStoryboard has a scene whose view controller is CoolnessViewController and that this is its initial view controller.
Then in your main code you would import Coolness and, to present your CoolnessViewController from the storyboard, you would say:
let s = UIStoryboard (
name: "CoolnessStoryboard", bundle: NSBundle(forClass: CoolnessViewController.self)
)
let vc = s.instantiateInitialViewController() as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
Note the strategy here. Work backwards from the goal. We need the instance of CoolnessViewController that is in the storyboard. To get that, we need a reference to that storyboard. To do that, we need to identify that storyboard. How? We can identify it by name and by the bundle that it is in. But how can we identify that bundle? We can identify the bundle by means of a class in that bundle. We have such a class, because we have imported the framework (import Coolness) and the class there is public (so we can speak of it).
I had the same problem. This is how I solved after a little research:
1 - I have a framework called "Messenger.framework"
2 - Inside it, I have a storyboard file called "Messenger.Storyboard"
3- My initial view controller is a UINavigationController with a rootViewController and my ChatController.
So this is how a present my framework's chat UIViewController:
- (void)presentChatController
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Messenger"
bundle:[NSBundle bundleForClass:ChatController.class]];
ChatController *controller = [storyboard instantiateViewControllerWithIdentifier:#"Chat"];
[self.navigationController pushViewController:controller animated:YES];
}
I hope this helps you.
If your framework name is MyFramework, and storyboard name is Main, with a viewcontoller with storyboard id vc
if let urlString = NSBundle.mainBundle().pathForResource("MyFramework", ofType: "framework", inDirectory: "Frameworks") {
let bundle = (NSBundle(URL: NSURL(fileURLWithPath: urlString)))
let sb = UIStoryboard(name: "Main", bundle: bundle)
let vc = sb.instantiateViewControllerWithIdentifier("vc")
self.showViewController(vc, sender: nil)
}
I have inculded the framework using cocoapods.
In Swift 5.5, Xcode 13 and iOS 15
if let urlString = Bundle.main.path(forResource: "MyFramework", ofType: "framework", inDirectory: "Frameworks") {
let bundle = (Bundle(url: NSURL(fileURLWithPath: urlString) as URL))
let sb = UIStoryboard(name: "Main", bundle: bundle)
let vc = sb.instantiateViewController(withIdentifier: "vc")
self.show(vc, sender: nil)
}