I have a class named Home
which is the parent class of my app. Now, I want to initialize this class somewhere so that I can access everything inside the class from wherever I want. The starting point of the app is RootViewController
. Should I initialize the app in the starting point? If yes, how should I do it so that it can be accessed from everywhere in the app?
As per my comment above, set a property on the AppDelegate class with the type Home, initialize it in application:didFinishLaunchingWithOptions. Now you can access this instance of home through the sharedApplication.delegate.
In AppDelegate.swift:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var myHome: Home?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
// Override point for customization after application launch.
self.myHome = Home()
return true
}
Then access it in some other class:
let delegate = UIApplication.sharedApplication().delegate as AppDelegate
var home = delegate.myHome
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.