IPhone Tech Talks/Developing iPhone Applications with UIKit
From ceejayoz
Matt Drance
Two types of view controllers
- Yours
- screenful of content
- custom UIViewController subclass
- one per screen
- Apple's
- navigation (header toolbar, provides back/forwards buttons)
- tab/toolbar (black footers)
- multiple screens, rarely subclassed
Contents |
[edit] UIViewController
key methods: (load these lazily!!!)
- loadView
- build heirarchy when NOT using Interface Builder
- viewDidLoad
- load additional resources/data
- not on screen yet, but ready
- doesn't fire every time view appears - it may be in memory already if previously loaded
- viewWillAppear
- on-screen reprep
- MAY not reload on every showing
- viewDidAppear
- animations, visual changes
Write controller classes in Xcode, lay out views in Interface Builder
- set your custom class
- load new controllers with initWithNibName:bundle:
Watch memory usage
- release stale resources
- reload when needed
- clear properties in dealloc
[edit] Apple View Controllers
[edit] Navigation controllers
- multiple levels of detail, LTR
- manages stack of view controllers
- "free" navigation bar built-in
- pulls each controller's title property
- easily customised
- uses pushViewController:animated: and popViewController:animated:
[edit] Key methods
- pushViewController:animated:
- popViewControllerAnimated:
- popToViewController:animated:
- popToRootViewControllerAnimated:
- setNavigationBarHidden:animated:
[edit] Tab bar controllers
- for explicit context changes between separate categoreis of content
- manages an array of controllers
- should be the *highest-level* view controller
- accepts all other types
- including navigation controllers
- comes with "free" tab bar, 'more' button, custom configuration
[edit] Key properties
- selectedIndex
- selectedViewController
- viewControllers
[edit] Tab icons
- monochrome image with an alpha mask
- iPhone applies appropriate colours (gray gradient, blue shine, etc.)
- 30x30 approximately (some leeway, is unscaled)
Design considerations
- pick a good default set of tabs
- keep it top-level
[edit] Toolbars
- for tools and actions on current screen
- present alert/modal views
- *not* for navigation (use tab bars)
[edit] Table (list) views
- plain or grouped (exact same code)
- header and footer
- defined sections
- table cells with data
- controlled by UITableViewController
[edit] Building table views
- controller as data source
- define sections and titles
- delegate receives numberOfSectionsInTableView message (for no section dividers, return 1, not 0)
- tableView:titleForHeaderInSection:
- define rows per section
- tableView:numberOfRowsInSection:
- define individual cells
- tableView:cellForRowAtIndexPath:
- if data changes, use reloadData to rebuild everything
[edit] UITableViewCell
- easy to customise
- image, text, and accessory view (usually the > icon, or the blue > icon for different function than clicking the row itself)
- ad subviews to contentview property, size for future content (to width of row, perhaps)
- give cells unique identifiers - cells, once no longer displayed, are queued and can often be restored w/o reinstantiation (via reuseIdentifier:cellID or Interface Builder's Inspector)
- keep subviews opaque!
- optimise data access for fast row counts
tableView:didSelectRowAtIndexPath: - respond to row selection tableView:accessoryButtonTappedForRowWithIndexPath - respond to blue accessory > button
[edit] Autorotation
- just need to implement
shouldAutorotateToInterfaceOrientation
[edit] In-call status bar
- twice the height
- view controller resizes automatically
[edit] Memory management
- each view controller receives memory warnings
- free heavy resources in didReceiveMemoryWarning
- always call super to remove offscreen views

