How It Fits Together ¢ Top view controller’s view ¢ Top view controller’s title... How It Fits Together ¢ Top view controller’s view ¢ Top view controller’s title ¢ Previous view cont
Trang 1CS193P - Lecture 7
iPhone Application Development Navigation & Tab Bar Controllers
Trang 2Announcements
¢ Assignment 3 was due last night at 11:59 PM
¢ Presence 1 is due on Tuesday 4/28
Trang 3Announcements
¢ Next Monday, 4/27
= Table Views, Scroll Views and Presence 2
- Guest speaker: Jason Beaver, UIKit Engineer
Trang 4Announcements
¢ This Friday:“Preparing Your App for the App Store”
¢ Next Friday: Loren Brichter of Tweetie (http://www.atebits.com)
F ¬
Trang 6Navigation Controllers
Trang 8How It Fits Together
¢ Top view controller’s view
Trang 9How It Fits Together
¢ Top view controller’s view
¢ Top view controller’s title
Trang 10How It Fits Together
¢ Top view controller’s view
¢ Top view controller’s title
¢ Previous view controller's title
Trang 11Modifying the Navigation Stack
¢ Push to add a view controller
- Cvoid)pushViewControLler: CUIViewControLller *)viewControLler
* Pop to remove a view controller
Trang 12Pushing Your First View Controller
- (void)appLicatitonDidFinishLaunching
// Create a navigation controLler
navControLler = [[UINavigationControLler alloc] init];
// Push the first view controller on the stack
[navControLLer pushViewControLler: firstVitewControl Ler
animated:NO];
// Add the navigation controlLler’s view to the window
[window addSubview:navControlLler.view];
b
Trang 13In Response to User Actions
¢ Push from within a view controller on the stack
° Almost never call pop directly!
- Automatically invoked by the back button
Trang 14Demo:
Pushing & Popping
Trang 15Application Data Flow
Trang 17A Controller for Each Screen
mm
Trang 18Connecting View Controllers
¢ Multiple view controllers may need to share data
¢ One may need to know about what another is doing
= Watch for added, removed or edited data
- Other interesting events
Trang 19How Not To Share Data
¢ Global variables or singletons
- This includes your application delegate!
¢ Direct dependencies make your code less reusable
- And more difficult to debug & test
Trang 20
Best Practices for Data Flow
¢ Figure out exactly what needs to be communicated
¢ Define input parameters for your view controller
¢ For communicating back up the hierarchy, use loose coupling
= Define a generic interface for observers (like delegation)
Controller
Trang 21
Example:
UllmagePickerController
Trang 22Demo:
Passing Data Along
Trang 23Customizing Navigation
Trang 24Customizing Navigation
¢ Buttons or custom controls
¢ Interact with the entire screen
Trang 25UINavigationltem
° Describes appearance of the navigation bar
- [itle string or custom title view
- Left & right bar buttons
- More properties defined in UINavigationBar.h
¢ Every view controller has a navigation item for customizing
= Displayed when view controller is on top of the stack
Trang 26Navigation Item Ownership
as
ee gl NH2,
Trang 27
Displaying a Title
¢ UlViewController already has a title property
- @propertyCnonatomic,copy) NSString *title;
¢ Navigation item inherits automatically
= Previous view controller's title is displayed in back button
viewControLLer.titLle = @“DetatL”;
Trang 28Left & Right Buttons
¢ UlBarButtonltem
= Special object, defines appearance & behavior for items in
navigation bars and toolbars
¢ Display a string, image or predefined system item
¢ Target + action (like a regular button)
Trang 29Text Bar Button Item
Trang 30System Bar Button Item
- (vo1id)viewDidLoad
{
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
1n1tW1thBarButtonSystemTtem: U[BarButtonSystemTtemAdd styLe:UTBarButtonTtemStyLeBordered
target: self action: @seLector(Cadd: ) ];
self navigationiItem.rightBarButtoniItem = addButton;
LaddButton release];
b
Trang 31Edit/Done Button
Carrier
° Very common pattern
¢ Every view controller has one available
- Target/action already set up
tes Cherry Cobbler
as
\W Chocolate Cake
self.nav1gattonTtem LeftBarButtonTtem = seLf.ediItButtonTtem;
// Called when the user toggles the edit/done button
- (void)setEditing: CBOOL)editing animated: CBOOL )animated
i
// Update appearance of views
b
Trang 32Custom Title View
¢ Arbitrary view in place of the title
Trang 33Back Button
¢ Sometimes a shorter back button is needed
self.title = @“HeLlo there, CS193P!”;
Trang 34Back Button
¢ Sometimes a shorter back button is needed
self.title = @“HeLlo there, CS193P!”;
UTBarButtonTtem *heyButton = [[UTBarButtonTtem aLLoc]
1n1tWtthT1tLe:@“Hey!”
ren
self navigationiItem.backButtonItem = heyButton;
[heyButton release];
Trang 35Demo:
Customizing Buttons
Trang 36Tab Bar Controllers
Trang 38How It Fits Together
Carrier ¢ Selected view controller’s view
Trang 39How It Fits Together
¢ Selected view controller’s view
"7 ¢ All view controllers’ titles
Trang 40Setting Up a Tab Bar Controller
- (void)appLicatitonDidFinishLaunching
b
// Create a tab bar controller
tabBarControLler = [[UITabBarControLler alloc] init];
// Set the array of view controllers
tabBarControLler.viewControLlers = myViewControllers;
// Add the tab bar controLller’s view to the window
Trang 41Tab Bar Appearance
¢ View controllers can define their appearance in the tab bar
H9 |0 c?000(210 1)
- Title + image or system item
° Each view controller comes with a tab bar item for customizing
Trang 42Creating Tab Bar Items
¢ Title and image
Trang 43Creating Tab Bar Items
Trang 44Demo:
Using a Tab Bar Controller
Trang 45More View Controllers
¢ What happens when a tab bar controller has too many
view controllers to display at once?
Trang 46More View Controllers
¢ What happens when a tab bar controller has too many
view controllers to display at once?
displayed automatically
- User can navigate to Most Recent
remaining view controllers W Top Rated
C History
.>x 9302 mài we TT
Featured Most Viewed Bookmarks Search More
Trang 47More View Controllers
¢ What happens when a tab bar controller has too many
view controllers to display at once?
displayed automatically
- User can navigate to
remaining view controllers na eare
= Customize order + ©
Most Recent Top Rated History
>„Š vô Ô.~
Search Featured Most Viewed Bookmarks More
Trang 48Combining Approaches
Trang 49Tab Bar + Navigation Controllers
Multiple parallel hierarchies
Search the Stanford directory, search campus map,
Flyin' Shoes find and bookmark courses, and get scores,
schedules, and news for all Stanford varsity athletics teams All from your iPhone or iPod Touch!
Friend Opportunity
Features:
- Search for buildings / places on campus, and use
Frontin' On Debra (DJ GPS to find your location
- Search for students, faculty or anybody in the Stanford directory Tap to call/email You can even
add them to your contacts on your phone!
- Search for classes, find out where and when they're offered, and tap to call or email the
a Leh) Artists Albums Podcasts More erties Categories Top 25 oer Fed
Fulfillingness' First Fi
Trang 50Tab Bar + Navigation Controllers
Trang 51
Nesting Navigation Controllers
¢ Create a tab bar controller
tabBarControLler = [[UITabBarControLler alloc] init];
¢ Create each navigation controller
navControLler = [LUINavigationControLler alloc] init];
[navControLLer pushViewControLler: firstViewControl Ler
animated:NO];
¢ Add them to the tab bar controller
tabBarControLLler.viewControLllLers = [NSArray arrayWithObjects:
navControLLer, anotherNavControlLer, someViewControLLer, aye
Trang 52Questions?