Here is my code in my app delegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = createInititalViewController()
self.window?.makeKeyAndVisible()
return true
}
func createInititalViewController() -> UINavigationController{
var currentStocksModel = CurrentStocksModel()
var currentStocksController = CurrentStocksViewController(model: currentStocksModel)
var navController:UINavigationController = UINavigationController(rootViewController: currentStocksController)
return navController
}
When I println(self.navigationController) in the CurrentStocksViewController, it returns nil. What am I forgetting to do?
This is where I am calling my println()
init(model:CurrentStocksModel) {
super.init()
self.model = model
stocksView = CurrentStocksView()
stocksView.delegate = self
self.view = stocksView
println(self.navigationController)
}
In your code you first create CurrentStocksViewController via init and after that you create UINavigationController with currentStocksViewController.
So, in the init method of CurrentStocksViewController, there is no UINavigationController. You should move the logic to viewDidLoad
Related
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
}
hi I followed some tutorial in stack for only seeing UIPageviewcontroller running at first time I find the best one and than implement it in app delegate .... first of all I have only one storyboard with two viewcontroller which first one is my PageViewController and second one is my Loginvc
I implemented this code on appdelegate and I get crash everything looks good but I get crash this is the code that I have implement it
var window: UIWindow?
var story : UIStoryboard?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarStyle = .lightContent
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let lunchedBefore = UserDefaults.standard.bool(forKey: "lunchedBefore")
if lunchedBefore {
story = UIStoryboard(name: "TShopUI", bundle: nil)
let rootcontroller = story?.instantiateViewController(withIdentifier: "LoginVC")
if let window = self.window {
window.rootViewController = rootcontroller
}
} else {
UserDefaults.standard.set(true, forKey: "lunchedBefore")
story = UIStoryboard(name: "TShopUI", bundle: nil)
let rootcontroller = story?.instantiateViewController(withIdentifier: "MainVC")
if let window = self.window {
window.rootViewController = rootcontroller
}
}
return true
}
thanks for every help
please answer as clear as you can I'm new to iOS development
It's crashing because of UserDefaults, you have to handle UserDefaults like below,
Updated: instead of Bool we can use object. So Whether the if condition satisfies, it's not a first launch. else it's first launch.
var window: UIWindow?
var story : UIStoryboard?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarStyle = .lightContent
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
if let lunchedBefore = UserDefaults.standard.object(forKey: "lunchedBefore") {
story = UIStoryboard(name: "TShopUI", bundle: nil)
let rootcontroller = story?.instantiateViewController(withIdentifier: "LoginVC")
if let window = self.window {
window.rootViewController = rootcontroller
}
} else {
UserDefaults.standard.set(true, forKey: "lunchedBefore")
story = UIStoryboard(name: "TShopUI", bundle: nil)
let rootcontroller = story?.instantiateViewController(withIdentifier: "MainVC")
if let window = self.window {
window.rootViewController = rootcontroller
}
}
return true
}
the problem was that I should set a storyboard ID for each view controller and than in delegate set their identifier
I downloaded sample code from here for a location tracking application https://www.pubnub.com/blog/2015-05-05-getting-started-ios-location-tracking-and-streaming-w-swift-programming-language/. I am trying to run the application but in the AppDelegate class I am getting an error saying "Class AppDelegate has no initializers". What is causing this error and how can I fix it?
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
// MARK: - Properties
//var window: UIWindow?
var window = UIWindow(frame: UIScreen.mainScreen().bounds)
// MARK: - App Life Cycle
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Adding a Navigation Controller and tool bar
self.window.rootViewController = UINavigationController(rootViewController: MainViewController(nibName: nil, bundle: nil))
// Make window visible
self.window.makeKeyAndVisible()
return true
}
}
I would set the window to be an optional value with no default. just as you originally had then commented out
var window: UIWindow?
then give window a value and programmatically add the root view controller when your app launches
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let viewController = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller
let navigationController = UINavigationController(rootViewController: viewController)
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
return true
}
I have a class in my SignInViewController.swift:
class CredentialState: NSObject {
static let sharedInstance = CredentialState()
var signedIn = false
var displayName: String?
var photoUrl: NSURL?
}
I would like to use the variable signedIn to authenticate users in AppDelegate with an if-else statement. Currently, I have a way to set the viewcontroller to CustomTabBarController (custom programmatically made) or SignInViewController (storyboard made). The if statement would basically say if the value is false set the controller to the sign in screen and if it's true then go to the tab bar screen.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.makeKeyAndVisible()
// Main view controller is inside of customtabbarcontroller, which gives a tab overlay
// window?.rootViewController = CustomTabBarController()
// Sets the main view to a storyboard element, such as SignInVC
let storyboard = UIStoryboard(name: "SignIn", bundle: nil)
let loginVC = storyboard.instantiateViewControllerWithIdentifier("SignInVC") as! SignInViewController
self.window?.rootViewController = loginVC
return true
}
if i understood you correctly:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.mainScreen().bounds)
// check login state
if CredentialState.sharedInstance.signedIn {
// Main view controller is inside of customtabbarcontroller, which gives a tab overlay
window?.rootViewController = CustomTabBarController()
} else {
// Sets the main view to a storyboard element, such as SignInVC
let storyboard = UIStoryboard(name: "SignIn", bundle: nil)
let loginVC = storyboard.instantiateViewControllerWithIdentifier("SignInVC") as! SignInViewController
window?.rootViewController = loginVC
}
window?.makeKeyAndVisible()
return true
}
I am not so sure of what you are asking, yet I'll try to answer that. Basically what you need to do is just simply have this piece of code above your CredentialState class:
credentialState : CredentialState = CredentialState()
In this way you can change or check your signedIn variable from AppDelegate. So simply in the AppDelegate file you can:
if(credentialState.signedIn == true) ...
Hope I was able to answer your question
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
}