代写 Announcements

Announcements
• Lab 1 regrade form will be posted on Piazza • Lab 2 is due tonight by 11:59 PM
– Late policy is 10% of lab total per day late (max 3 days late, after that it is a zero)
• So -7.5 points per day late for lab 2
• Labs 3 and 4 are posted on the course website
1E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 1
Today’s Topics
• Finish Circle Drawing App
• Designing iPhone Applications
• Model-View-Controller (Why and How?) • View Controllers
2E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 2

Circle Drawing App
3E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 3
Designing iPhone Applications
4E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 4

Different Flavors of Mail
5E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 5
Organizing Content
6E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 6

Organinzing Content
• Focus on your user’s data
• One thing at a time
• Screenfuls of content
7E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 7
Patterns for Organizing Content
Navigation Bar Tab Bar
8E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 8

Navigation Bar
• Hierarchy of content
• Drill down into greater detail
9E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 9
Tab Bar
• Self-contained modes
10E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 10

A Screenful of Content
• Slice of your application • Views, data, logic
11E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinlicgatPiolnatDfoevrmelopment 11
Parts of a Screenful
12E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 12

Parts of a Screenful
13E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 13
Model-View-Controller (Why and How?)
14E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 14

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
15E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 15
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
16E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 16

Communication and MVC
KVO, notifications
KVO, notifications
target-action, delegation
17E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 17
View Controllers
18E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 18

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
19E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 19
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
20E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 20

UIViewController
• Basic building block
• Manages a screenful of content
• Subclass to add your application logic
View Controller
21E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 21
􏰀Your􏰁 and Apple View Controllers
• Create your own UIViewController subclass for each screenful
• Plug them together using existing composite view controllers
22E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 22

􏰀Your􏰁 and 􏰀Our􏰁 View Controllers
• Create your own UIViewController subclass for each screenful
• Plug them together using existing composite view controllers
23E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 23
Your View Controller Subclass
import UIKit
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
} }
24E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 24

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
25E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 25
When to call -loadView?
• Don’t do it!
• Cocoa tends to embrace a lazy philosophy
– Call setNeedsDisplay() instead of draw(_:)
• Allows work to be deferred – Performance!
• Consider time to launching an application
26E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 26

View Controller Lifecycle
override func viewDidLoad() {
// Your view has been loaded // Customize it here if needed
view.someWeirdProperty = true }
27E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 27
View Controller Lifecycle
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Your view is about to show on the screen
beginLoadingDataFromTheWeb()
startShowingLoadingProgress() }
28E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 28

View Controller Lifecycle
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Your view is about to leave the screen
rememberScrollPosition()
saveDataToDisk() }
29E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 29
Navigation View Controller Demo
30E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 30

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
31E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 31
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
32E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 32

UserDefaults Demo
33E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 33