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
Related
Hello i have done a page controller for my app.
This page controller will appear at the app launch if there isn't any "User" core data object on the device.
So want i want to do is :
When app launch, do a core data request that return all "User" objects in a array. (this part is ok)
If the array is empty the VC presented is the pageController else that's the normal on whose presented.
The pageController and the other controller are on different storyboards.
I have seen we can change the first storyboard in the app parameter, that's parameter is called main interface.
Actually main interface is the normal VC, so how can i present the page controller programmaticly in my didFinishLaunchingWithOptions() when my condition is true ?
Inside didFinishLaunchingWithOptions
// query coredata
if arr.isEmpty {
let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "pageID") as! PageController
self.window?.rootViewController = viewController
}
else {
let otherController = UIStoryboard(name: "Other", bundle: nil).instantiateViewController(withIdentifier: "otherID") as! OtherController
self.window?.rootViewController = otherController
}
I am trying to check the network status from the AppDelegate.swift instead of checking from LaunchViewController.
I have written showOfflinePage in AppDelegate when I shut down my network I have this error cast
Could not cast value of type
'reachability_playground.LaunchViewController' (0x1095b5f20) to
'UINavigationController' (0x114bb2a20). 2019-01-06
16:42:04.079430-0500 reachability-playground[2781:93635] Could not
cast value of type 'reachability_playground.LaunchViewController'
(0x1095b5f20) to 'UINavigationController' (0x114bb2a20).
private func showOfflinePage() -> Void {
DispatchQueue.main.async {
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let viewController: LaunchViewController = storyboard.instantiateViewController(withIdentifier: "LaunchViewController") as! LaunchViewController;
// Then push that view controller onto the navigation stack
let rootViewController = self.window!.rootViewController as! UINavigationController
rootViewController.pushViewController(viewController, animated: true);
}
}
As per the exception message, your root view controller is an instance of LaunchViewController, not an instance of UINavigationController so the forced downcast fails.
You need to check your storyboard and ensure that the entry point scene is a navigation controller.
I am using the storyboard references in my project. When I tried to load view controller using self.storyboard it crashes. But When I created a new instance of storyboard and tried to load it works perfectly.
Another thing is without storyboard reference it works perfectly for the first case also.
I am using Xcode 7.3.1.
With self.storyboard. It crashes
guard let registerViewController = self.storyboard?.instantiateViewControllerWithIdentifier(StoryboardIdentifier.Registration) as? RegisterVC else { return }
This is the instance of UIStoryboard. It works.
let mainStoryBoard = UIStoryboard.init(name: "Main", bundle: nil)
guard let registerViewController = mainStoryBoard.instantiateViewControllerWithIdentifier(StoryboardIdentifier.Registration) as? RegisterVC else {
return
}
I'm going to go out on a limb here, since you are using Storyboard References, and say that mainStoryBoard and self.storyboard are referencing different StoryBoards, and the one that chokes does not contain the ViewController named StoryboardIdentifier.Registration.
Either that or StoryboardIdentifier.Registration contains a different value in each case.
EDIT: I was off base in my approach. I'm trying to pass data from one VC to another. The source VC is not controlled by a Tab Bar, so I can't reference the destination VC through it. I am attempting to reference the destination VC thorough the Storyboard, but the data is still nil on the destination VC. I'm using Core Data and wish to pass the ManagedObjectContext to another VC. Here is the code:
......
do {
try self.managedObjectContext.save()
appUserID = user.userID!
} catch {
fatalError("Error: \(error)")
}
}
}
self.passManagedObjectContext()
}
func passManagedObjectContext() {
let profileTableViewController = UIStoryboard(name: "Profile", bundle: nil).instantiateViewController(withIdentifier: "ProfileViewController") as! ProfileTableViewController
profileTableViewController.managedObjectContext = self.managedObjectContext
}
I am passing data from the AppDelegate to other ViewControllers by referencing my TabBarViewControllers stack like so:
let tabBarController = window!.rootViewController as! UITabBarController
if let tabBarViewControllers = tabBarController.viewControllers {
let profileNavController = tabBarViewControllers[4] as! UINavigationController
let profileTableViewController = profileNavController.topViewController as! ProfileTableViewController
profileTableViewController.managedObjectContext = context
}
}
.....
END EDIT
I'm trying to do the same now but from one ViewController to another that is accessed through the tab bar (no segue). The code above does not recognize window!.rootViewController how can I reference the root UITabBarController?
UIViewController has a built-in optional property tabBarController, so inside your view controller you can access the tabBarController like so:
if let tabBarController = tabBarController {
//get the controller you need from
//tabBarController.viewControllers
//and do whatever you need
}
Here is the current list of properties available in the 'Getting Other Related View Controllers' section of Apple's UIViewController documentation:
I am currently writing an app in Swift and I implemented all my UI programatically. I would it kind of exhausting, so recently I decided to use storyboard but I am stuck with a bug.
When passing over the mainContext from the AppDelegate to the FirstViewController, the value of the mainContext is nil. In the first place, when I wrote everything programmatically, my code looked like this:
let vc = FirstViewController()
vc.mainContext = mainContext
window?.rootViewController = vc
But now that I am working with StoryBoard, this is not valid anymore because I have to instantiate the view with its ID, like this:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var vc = storyboard.instantiateViewControllerWithIdentifier("FirstView") as! FirstViewController
vc.mainContext = mainContext
But in this case, the context is nil. If I don't instantiate the VC like this, my outlets are nil and I get this error:
fatal error: unexpectedly found nil while unwrapping an Optional value
Does someone know a way to pass my mainContext?
You're not accessing the rootViewController, you're creating a new view controller and not doing anything with it, to access the rootViewController replace this
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var vc = storyboard.instantiateViewControllerWithIdentifier("FirstView") as! FirstViewController
vc.mainContext = mainContext
with
if let vc = self.window?.rootViewController as? FirstViewController {
vc.mainContext = mainContext
}