Announcements Lab 2 is due tonight by 11:59 PM Late policy is 10% of lab total per day late So -7.5 points per day late for lab 2 Labs 3 and 4 are posted on the course website Extensible Networking Platform 1 1 - CSE 438 Mobile Application Development Today s Topics Finish Circle Drawing App Designing iphone Applications Model-View-Controller (Why and How?) View Controllers Extensible Networking Platform 2 2 - CSE 438 Mobile Application Development
Circle Drawing App Extensible Networking Platform 3 3 - CSE 438 Mobile Application Development Designing iphone Applications Extensible Networking Platform 4 4 - CSE 438 Mobile Application Development
Different Flavors of Mail Extensible Networking Platform 5 5 - CSE 438 Mobile Application Development Organizing Content Extensible Networking Platform 6 6 - CSE 438 Mobile Application Development
Organinzing Content Focus on your user s data One thing at a time Screenfuls of content Extensible Networking Platform 7 7 - CSE 438 Mobile Application Development Patterns for Organizing Content Navigation Bar Tab Bar Extensible Networking Platform 8 8 - CSE 438 Mobile Application Development
Navigation Bar Hierarchy of content Drill down into greater detail Extensible Networking Platform 9 9 - CSE 438 Mobile Application Development Tab Bar Self-contained modes Extensible Networking Platform 10 10 - CSE 438 Mobile Application Development
A Screenful of Content Slice of your application Views, data, logic Extensible Networking Platform 11 11 - CSE 438 Mobile Application Development Parts of a Screenful Extensible Networking Platform 12 12 - CSE 438 Mobile Application Development
Parts of a Screenful Extensible Networking Platform 13 13 - CSE 438 Mobile Application Development Model-View-Controller (Why and How?) Extensible Networking Platform 14 14 - CSE 438 Mobile Application Development
Why Model-View-Controller? Ever used the word spaghetti to describe code? Clear responsibilities make things easier to maintain Avoid having one monster class that does everything Extensible Networking Platform 15 15 - CSE 438 Mobile Application Development Why Model-View-Controller? Separating responsibilities also leads to reusability By minimizing dependencies, you can take a model or view class you ve already written and use it elsewhere Think of ways to write fewer lines of code Extensible Networking Platform 16 16 - CSE 438 Mobile Application Development
Communication and MVC KVO, notifications KVO, notifications target-action, delegation Extensible Networking Platform 17 17 - CSE 438 Mobile Application Development View Controllers Extensible Networking Platform 18 18 - CSE 438 Mobile Application Development
Problem: Managing a Screenful Controller manages views, data and application logic Apps are made up of many of these Would be nice to have a well-defined starting point A la UIView for views Common language for talking about controllers Extensible Networking Platform 19 19 - CSE 438 Mobile Application Development Problem: Building Typical Apps Some application flows are very common Navigation-based Tab bar-based Combine the two Don t reinvent the wheel Plug individual screens together to build an app Extensible Networking Platform 20 20 - CSE 438 Mobile Application Development
UIViewController Basic building block Manages a screenful of content Subclass to add your application logic View Controller Extensible Networking Platform 21 21 - CSE 438 Mobile Application Development Your and Apple View Controllers Create your own UIViewController subclass for each screenful Plug them together using existing composite view controllers Extensible Networking Platform 22 22 - CSE 438 Mobile Application Development
Your and Our View Controllers Create your own UIViewController subclass for each screenful Plug them together using existing composite view controllers Extensible Networking Platform 23 23 - CSE 438 Mobile Application Development import UIKit Your View Controller Subclass class MyViewController : UIViewController { // A view controller will usually // manage views and data var mydata = [String]() // And respond to actions @IBAction func dosomeaction(_ sender: UIButton) { // Do something here when button pressed } } Extensible Networking Platform 24 24 - CSE 438 Mobile Application Development
The View in View Controller UIViewController superclass has a view property view: UIView Loads lazily On demand when requested OS figures this out Can be purged on demand as well (low memory) Sizing and positioning the view? Depends on where it s being used Don t make assumptions, be flexible Extensible Networking Platform 25 25 - CSE 438 Mobile Application Development Don t do it! When to call -loadview? Cocoa tends to embrace a lazy philosophy Call setneedsdisplay() instead of draw(_:) Allows work to be deferred Performance! Consider time to launching an application Extensible Networking Platform 26 26 - CSE 438 Mobile Application Development
View Controller Lifecycle override func viewdidload() { // Your view has been loaded // Customize it here if needed view.someweirdproperty = true } Extensible Networking Platform 27 27 - CSE 438 Mobile Application Development View Controller Lifecycle override func viewwillappear(_ animated: Bool) { super.viewwillappear(animated) // Your view is about to show on the screen beginloadingdatafromtheweb() startshowingloadingprogress() } Extensible Networking Platform 28 28 - CSE 438 Mobile Application Development
View Controller Lifecycle override func viewwilldisappear(_ animated: Bool) { } super.viewwilldisappear(animated) // Your view is about to leave the screen rememberscrollposition() savedatatodisk() Extensible Networking Platform 29 29 - CSE 438 Mobile Application Development Navigation View Controller Demo Extensible Networking Platform 30 30 - CSE 438 Mobile Application Development
Loading and Saving Data Lots of options out there, depends on what you need UserDefaults Property lists SQLite Web services Covering in greater depth in a few lectures Extensible Networking Platform 31 31 - CSE 438 Mobile Application Development Saving State Across App Launches UserDefaults (renamed from NSUserDefaults) to read and write prefs & state Singleton object: UserDefaults.standard Methods for storing & fetching common types: integer(forkey: String) set(value: Int, forkey: String) Find an appropriate time to store and restore your state Extensible Networking Platform 32 32 - CSE 438 Mobile Application Development
UserDefaults Demo Extensible Networking Platform 33 33 - CSE 438 Mobile Application Development