CS193P - Lecture 12 iPhone Application Development Text Input Presenting Content Modally
Announcements • Presence 3 assignment out today, due Wednesday 11/5 • Enrolled students can (still) pick up an iPod Touch after class • Final project proposals due on Monday 1/1
Today’s Topics • Notes & Errata • Drawing Optimizations (continued from Tuesday) • iPhone Keyboards • Customizing Text Input • Presenting Content Modally
Notes & Errata
Using UINavigationController • Don’t add a child view controller’s view manually UIViewController *viewController = ...; [navigationController.view addSubview:viewController.view];
• Push a view controller to display it UIViewController *viewController = ...; [navigationController pushViewController:viewController ! animated:YES];
When To Call -init • Call -init (or variants thereof ) only once on an object. Ever. • Seriously.
Operations and Autorelease Pools • If you detach a thread using NSThread, you need to create
your own autorelease pool... • With NSOperation and NSOperationQueue, it’s done for you
Drawing Optimizations (Continued from Tuesday)
Draw Lazily • Never call -drawRect: directly • Invoke -setNeedsDisplay !
Or even better, -setNeedsDisplayInRect:
• In your -drawRect: implementation, only do the work required for the specified rect
Compose with Image Views • When drawing large images on the screen, don’t use a custom
view with an override of -drawRect: • UIImageView has built-in optimizations for speed and memory Memory mapping reduces your footprint ! Doesn’t copy image data to draw !
Avoid Transparency When Possible • Opaque views are much faster to draw than transparent views • Especially important when scrolling • This is the default- introduce transparency with care
Reusing Table View Cells • Memory churn can affect smoothness of scrolling • UITableView provides mechanism for reusing table view cells - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // Ask the table view if it has a cell we can reuse UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (!cell) { // If not, create one with our identifier cell = [[UITableViewCell alloc] initWithFrame:CGRectZero identifier:MyIdentifier]; [cell autorelease]; } return cell; }
More on Optimizing Drawing • “iPhone Application Programming Guide - Drawing Tips”
https://developer.apple.com/iphone/library/documentation/ iPhone/Conceptual/iPhoneOSProgrammingGuide/ GraphicsandDrawing/chapter_6_section_3.html
Performance Recap • Performance is both an art and a science !
Combine tools & concrete data with intuition & best practices
• Be frugal with memory • Concurrency is tricky, abstract it if possible • Drawing is expensive, avoid unnecessary work
iPhone Keyboards
Virtual keyboard Appears when needed
Virtual keyboard Appears when needed
Portrait and Landscape
Simple selection model Text loupe/magnifier
Many keyboard types Adapted to task
Many keyboard types Adapted to task
Many keyboard types Adapted to task
Many keyboard types Adapted to task
Many keyboard types Adapted to task
Many keyboard types Adapted to task
Single line editing
Multi-line editing
20 Languages Full dictionary support
English
French
Russian
Korean
Japanese Romaji
Japanese Kana
Chinese Pinyin
Chinese Handwriting Simplified Traditional
Customizing Text Input
Text Containers
Text Containers Delegates Notifications Methods
Text Containers Text Input Traits
Protocol Text Input Traits
UITextField UITextView
Autocapitalization Autocorrection Keyboard Type Text Input Traits
Keyboard Appearance Return Key Type Return Key Autoenabling Secure Text Entry
Text Input Traits
URL Keyboard Go button
Text Input Traits
Default Keyboard Google button
Text Containers Text Input Traits Delegates Notifications Methods
Text Containers
UITextField
Design time
UITextField URL Keyboard Go button
Design time
UITextField URL Keyboard Go button
Run time
UITextField URL Keyboard Go button
Become first responder
Keyboard
UITextField URL Keyboard Go button
Become first responder
Keyboard
UITextField
URL Keyboard Go button
URL Keyboard Go button
Keyboard adopts traits
UITextField Text Containers
UITextView Web Forms
Demo: Text Input
Presenting Content Modally
Presenting Content Modally • For adding or picking data
Presenting // Recipe list view controller - (void)showAddRecipe { RecipeAddViewController *viewController = ...; [self presentModalViewController:viewController animated:YES]; }
Presenting // Recipe list view controller - (void)showAddRecipe { RecipeAddViewController *viewController = ...; [self presentModalViewController:viewController animated:YES]; }
Dismissing // Recipe list view controller - (void)didAddRecipe { [self dismissModalViewControllerAnimated:YES]; }
Dismissing // Recipe list view controller - (void)didAddRecipe { [self dismissModalViewControllerAnimated:YES]; }
Separate Navigation Stacks
Separate Navigation Stacks
Separate Navigation Stacks
Dismissing Modally • Who should do it? • Best practice is for the same object to call present and dismiss • Define delegate methods for the presented controller Tell the delegate when the presented controller is done ! The delegate makes the call to dismiss !
Parent Controller
Present
Child Controller
Dismissing Modally • Who should do it? • Best practice is for the same object to call present and dismiss • Define delegate methods for the presented controller Tell the delegate when the presented controller is done ! The delegate makes the call to dismiss !
Parent Controller I’m done!
Child Controller
Dismissing Modally • Who should do it? • Best practice is for the same object to call present and dismiss • Define delegate methods for the presented controller Tell the delegate when the presented controller is done ! The delegate makes the call to dismiss !
Parent Controller
Dismiss
Child Controller
Presence - Part 3
Goals for Presence 3 • Avoid expensive work on the main thread Use background threads to keep interface responsive ! Abstract thread lifecycle with NSOperation & NSOperationQueue !
• Allow the user to update their own status Text input with the keyboard ! Present a view controller modally !
Demo: Presence 3
Questions?