Accessing Tableview in splitViewController from AppDelegate.swift - ios

I have searched much but with no success. I am attempting to access the TableView of the splitViewController from AppDelegate.swift. How do I do this?
This is the standard code that Apple supplies when starting a new project for a Split View Controller:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let splitViewController = self.window!.rootViewController as! UISplitViewController
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem()
splitViewController.delegate = self
return true
}
Or perhaps it is not possible? Which I cannot believe :-)

Related

Catalyst Sidebar

I'm working through this tutorial from apple. I am getting stuck on the shown below, I keep getting an error, Use of unresolved identifier 'window'
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let splitViewController = window!.rootViewController as! UISplitViewController
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem
// Add a translucent background to the primary view controller.
splitViewController.primaryBackgroundStyle = .sidebar
splitViewController.delegate = self
return true
}
any suggestions?
It is simple. Catalyst apps are by default Window Scene-based. You don’t have a window here. Apple tutorials are outdated.
The solution is to create your own window and fill it in didFinish...
The more correct way is to add your code to delegate of scenes.

How can I pass data loaded when my application finished launching thru my UITabBar to a UITableView?

I am a new iOS programmer and I am trying to make an application that involves the following flow:
-> UITabBar -> UINavController -> UITableViewController.
Initially the program was working when I have the following flow:
-> UINavController -> UITableViewController
But when I added the UITabBar (with the Embed In method)the I had two issues:
1) Casting the initial view from UITableView to UITabBarView
2) The data which have been restored from Archives of the phone are not loading in the TableView.
I managed to fix the casting issue with the UIStoryboard IDs, but I am not sure if this way I created the second problem of not passing data to the UITableView correctly.
The Casting problem has taking place at the appDelegate code. Here is the original code I had before incorporating the UITabBarView:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let navController = window!.rootViewController as! UINavigationController
let SLBprojectController = navController.topViewController as! GR8TableView
SLBprojectController.SLBprojectDB = thisSLBprojectDB
return true
}
The problem with the above code was that it told me that it could not cast a TableViewController (GR8TableView) into a UITabBarView. I have managed to fix this by searching in the StackOverflow forums by making the following:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let tabBarIntial : UITabBarController = mainStoryboardIpad.instantiateViewController(withIdentifier: "TabBar") as! UITabBarController
let navigationController:UINavigationController = mainStoryboardIpad.instantiateViewController(withIdentifier: "navController") as! UINavigationController
let navigationController1:UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "ViewController1")
let SLBprojectController = navigationController.topViewController as! GR8TableView
SLBprojectController.SLBprojectDB = thisSLBprojectDB
tabBarIntial.viewControllers = [navigationController, navigationController1]
tabBarIntial.selectedIndex = 0
return true
}
But after I fixed the "casting" issue then I am getting problems loading the data in the TableView. Not sure if this problem is related to the way I fixed the casting issue.
Any assistance would be much much appreciated!
The simplest answer would be to go back to logic similar to what worked for you, but with a slightly more complex relationship. For example, I created an app with a tab bar controller, a navigation controller in the first tab, and a FirstViewController embedded in the navigation controller.
Since I don't have a database, I gave the FirstViewController a single instance variable called running.
The following code finds the controller and sets the variable:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let tabController = window!.rootViewController as? UITabBarController {
if let navController = tabController.viewControllers![0] as? UINavigationController {
if let firstController = navController.topViewController as? FirstViewController {
firstController.running = true
} else {
print("Unexpected controller in navigation hierarchy: \(String(describing: navController.topViewController))")
}
} else {
print("Unexpected contained controller: \(String(describing: tabController.viewControllers![0]))")
}
} else {
print("Unexpected root controller: \(String(describing: window!.rootViewController))")
}
return true
}
Note that I'm not creating any new controllers. That's all done by default storyboard loading.
I believe you should be able to adapt this to your own variable and class names.

iOS state restoration issue with DrawerController

I have an app written in Swift 3.1, using Xcode 8.3.3.
I am currently trying to implement state preservation/restoration.
To do this I have implemented shouldSaveApplicationState and willFinishLaunchingWithOptions methods in AppDelegate.swift and set to return true:
// AppDelegate.swift
// STATE RESTORATION CALLBACKS
func application(_ application: UIApplication, shouldSaveApplicationState coder: NSCoder) -> Bool {
debug_print(this: "shouldSaveApplicationState")
return true
}
func application(_ application: UIApplication, shouldRestoreApplicationState coder: NSCoder) -> Bool {
debug_print(this: "shouldRestoreApplicationState")
restoringState = true
return true
}
func application(application: UIApplication, willFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
debug_print(this: "willFinishLaunchingWithOptions")
return true
}
I’ve also provided restoration IDs for all involved viewcontrollers and navigationcontrollers.
I'm using a 3rd party library to handle side drawer navigation container (https://github.com/sascha/DrawerController). The initial viewcontroller is set programmatically inside the didFinishLaunchingWithOptions method, see below.
// AppDelegate.swift
var centerContainer: DrawerController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let centerViewController = mainStoryboard.instantiateViewController(withIdentifier: "RootViewControllerNav") as! UINavigationController
let leftViewController = mainStoryboard.instantiateViewController(withIdentifier: "SideDrawerViewController") as! UITableViewController
centerContainer = DrawerController(centerViewController: centerViewController, leftDrawerViewController: leftViewController)
centerContainer?.restorationIdentifier = "DrawerControllerView"
window = UIWindow(frame: UIScreen.main.bounds)
window?.restorationIdentifier = "MainWindow"
window?.rootViewController = centerContainer
window?.makeKeyAndVisible()
return true
}
When app opens and attempts to restore state, it displays the correct viewcontroller (last controller before app closed) temporarily, then once app becomes active it reverts back to the initial viewcontroller.
For example, the following happens:
Open app Navigate to the “settings” view via the side menu
Navigate to the home screen
Stop running xcode and start it again
App will open showing settings view, then revert back to home view
Can anyone tell me what is causing this, or where I am going wrong? Let me know if you need any more code examples.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let leftSideDrawerViewController = mainStoryboard.instantiateViewController(withIdentifier: "SideDrawerViewController") as! UITableViewController
let centerViewController = mainStoryboard.instantiateViewController(withIdentifier: "RootViewControllerNav") as! UINavigationController
let navigationController = UINavigationController(rootViewController: centerViewController)
navigationController.restorationIdentifier = "navigationControllerKey"
let leftSideNavController = UINavigationController(rootViewController: leftSideDrawerViewController)
leftSideNavController.restorationIdentifier = "LeftNavigationController"
self.drawerController = DrawerController(centerViewController: navigationController, leftDrawerViewController: leftSideNavController, rightDrawerViewController: nil)
self.drawerController.openDrawerGestureModeMask = .all
self.drawerController.closeDrawerGestureModeMask = .all
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = self.drawerController
return true
}

Swift - Setting up Dependency Injection in AppDelegate.swift

I am a self-taught newcomer to iOS programming and have worked through one beginner textbook. I'm trying to publish my first mobile application so I am going back through and cleaning up my code. The textbook I used stressed 'Dependency Injection' but I've struggled adapting their simple example to a more complex application.
The app operates as a shell and retrieves/parses txt files to populate. I successfully connected my model, which retrieves/parses the data, and the TableViewController that needs populated using the following code:
MyTableViewController {
var data: Data!
}
AppDelegate {
var window: UIWindow?
let data = Data()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let navCon = window!.rootViewController as! MyNavigationController
let tableVC = navCon.topViewController as! MyTableViewController
tableVC.data = data
return true
}
I then embedded that NavigationController within a TabBarController because the app will have other tabs. I tried the same process of setting the rootViewController and then drilling down until I could set my data variable, but I can't find the correct way to layer the ViewControllers and keep getting the error;
'fatal error: unexpectedly found nil while unwrapping an Optional value'
I tried two different approaches:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let tabBarCon = window!.rootViewController as! MyTabBarController
let navCon = tabBarCon.presentedViewController as! MyNavigationController
let tableVC = navCon.topViewController as! MyTableViewController
tableVC.data = data
return true
}
and
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let tabBarCon = window!.rootViewController as! MyTabBarController
let navCon = MyNavigationController()
tabBarCon.viewControllers = [navCon]
let tableVC = navCon.topViewController as! MyTableViewController
tableVC.data = data
return true
}
Is there a solution to correct this error or am I going about this process wrong? Again, I have a file that pulls in a txt file and then creates a dictionary. I need a separate TableViewController to be able to access that dictionary to populate itself, but I want to achieve this in the most efficient and apple promoted manner, not all in the same file as I did in my first design.
Thanks for the help!
For Dependency Injection I do recommend you use Typhoon. From mine experience it's one of the best tool. This will help you to achive app asembly like:
/*
* This is the definition for our AppDelegate. Typhoon will inject the specified properties
* at application startup.
*/
public dynamic func appDelegate() -> AnyObject {
return TyphoonDefinition.withClass(AppDelegate.self) {
(definition) in
definition.injectProperty("cityDao", with: self.coreComponents.cityDao())
definition.injectProperty("rootViewController", with: self.rootViewController())
}
}
I found the solution through this thread;
Aassigning a value to a view controller from AppDelegate.swift
The result can be achieved by reconfiguring the second solution I attempted;
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let tabBarCon = window!.rootViewController as! MyTabBarController
let tabBarRootVCs: Array = tabBarCon.viewControllers!
let navCon = tabBarRootVCs[0] as! MyNavigationController
let tableVC = navCon.topViewController as! MyTableViewController
tableVC.data = data
return true
}

How to set the rootViewController with Swift, iOS 7

I want to set the rootViewController in the app delegate ..
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
var rootView: MyRootViewController = MyRootViewController()
//Code to set this viewController as the root view??
return true
}
If you are using a storyboard and want to set your rootViewController programmatically, first make sure the ViewController has a Storyboard ID in the Identity Inspector.
Then in the AppDelegate do the following:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// get your storyboard
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// instantiate your desired ViewController
let rootController = storyboard.instantiateViewControllerWithIdentifier("MyViewController") as! UIViewController
// Because self.window is an optional you should check it's value first and assign your rootViewController
if let window = self.window {
window.rootViewController = rootController
}
return true
}
You can do something like this.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
var rootView: MyRootViewController = MyRootViewController()
if let window = self.window{
window.rootViewController = rootView
}
return true
}
Swift 2.0:
var window: UIWindow?
var storyboard:UIStoryboard?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.makeKeyAndVisible()
storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootController = storyboard!.instantiateViewControllerWithIdentifier("secondVCID")
if let window = self.window {
window.rootViewController = rootController
}
In order to get it to show there are some things you need to do if you are not using a storyboard. Inside the AppDelegate inside the function application.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let frame = UIScreen.mainScreen().bounds
window = UIWindow(frame: frame)
let itemsViewControler: UITableViewController = BNRItemsViewController()
if let window = self.window{
window.rootViewController = itemsViewControler
window.makeKeyAndVisible()
}
return true
}
if let tabBarController = self.window!.rootViewController as? UITabBarController
{
tabBarController.selectedIndex = 0
}

Resources