iOS View Controllers - kgleong/software-engineering GitHub Wiki
View Controllers
Lifecycle Methods
viewWillAppear
override func viewWillAppear(animated: Bool) {
// Start any tasks such as polling timers.
// E.g., a poller that fetching images from an external service.
super.viewWillAppear(animated)
}
didReceiveMemoryWarning
override func didReceiveMemoryWarning() {
/*
iOS will send a warning to an app before it kills the app's
process. This is an opportunity to free memory to avoid
termination.
E.g., clearing image caches.
*/
super.didReceiveMemoryWarning()
}
loadView
override func loadView() {
// Instantiate a custom view and set the controller's root view.
let customView = MyCustomView()
view = customView
}
Changing the root view controller
// In AppDelegate.swift
/*
`window` is an optional public property that the
UIApplicationDelegate protocol defines.
when the application instantiates the app delegate class,
it looks for the app delegate's `window` property.
if `nil`, it creates the window.
*/
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Remove the custom iOS target property: `Main storyboard file base name`
// since the main storyboard is not being used to specify the app's entry point.
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
// Creates an instance of a controller that contains IBOutlets.
// I.e., any controller that connects properties to object in a storyboard
// or xib/nib file.
let homeController =
storyboard.instantiateViewControllerWithIdentifier(
"homeController"
) as! HomeController
/*
A UIScreen contains properties associated with a display (hardware)
An iOS device has one main screen and optional attached screens.
`UIScreen#bounds` returns a `CGRect`, which describes the screen dimensions in points.
*/
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
if let window = self.window {
window.rootViewController = homeController
window.makeKeyAndVisible()
}
return true
}
References
References