CS193P - Lecture 7
Trang 2Announcements
¢ Assignment 3 was due last night at 11:59 PM
Trang 3Announcements
¢ Next Monday, 4/27
= Table Views, Scroll Views and Presence 2
Trang 4Announcements
¢ This Friday:“Preparing Your App for the App Store”
¢ Next Friday: Loren Brichter of Tweetie (http://www.atebits.com)
Trang 8Carrier John Appleseed Kate Bell Anna Haro Daniel Higgins Jr David Taylor Hank M Zakroff
How It Fits Together
Trang 9Carrier John Appleseed Kate Bell Anna Haro Daniel Higgins Jr David Taylor Hank M Zakroff
How It Fits Together
Trang 10Carrier John Appleseed Kate Bell Anna Haro Daniel Higgins Jr David Taylor Hank M Zakroff
How It Fits Together
¢ Top view controller’s view ¢ Top view controller’s title
Trang 11Modifying the Navigation Stack
¢ Push to add a view controller
- Cvoid)pushViewControLler: CUIViewControLller *)viewControLler
animated: CBOOL )animated;
* 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];
Trang 13In Response to User Actions
¢ Push from within a view controller on the stack - (void)someAction: (1d)sender i // Potentially create another view controller UIViewControLler *viewController = .; L[self.navigationControLLer pushViewControLller: viewControlLler animated: YES]; b
° Almost never call pop directly!
Trang 14Demo:
Trang 17A Controller for Each Screen
Trang 18Connecting View Controllers
¢ Multiple view controllers may need to share data
¢ One may need to know about what another is doing
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 20Best 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:
Trang 22Demo:
Trang 24Customizing Navigation
¢ Buttons or custom controls
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
Trang 26Navigation Item Ownership
as
ee gl NH2,
Trang 27Displaying 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
Trang 28
Left & Right Buttons
¢ UlBarButtonltem
= Special object, defines appearance & behavior for items in
navigation bars and toolbars
¢ Display a string, image or predefined system item
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
Trang 32Custom Title View
Trang 33Back Button
¢ Sometimes a shorter back button is needed
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;
Trang 35Demo:
Trang 38How It Fits Together
Trang 39How It Fits Together
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
World Clock Alarm Stopwatch Timer
H9 |0 c?000(210 1)
- Title + image or system item
Trang 42Creating Tab Bar Items
Trang 44Demo:
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?
= “More” tab bar item ——
displayed automatically
- User can navigate to Most Recent
remaining view controllers W Top Rated
C History
.>x 9302 mài we TT
Trang 47More View Controllers
¢ What happens when a tab bar controller has too many
view controllers to display at once?
= “More” tab bar item ——
displayed automatically
- User can navigate to
Trang 49Tab Bar + Navigation Controllers
Multiple parallel hierarchies iStanford Fingerlings 3 3 Stanford University W W* W* W W 9reviews „ Flight of the Conchords iStanford is Stanford University in the palm of your hand
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
= Wn Ko 86.5
a Leh) Artists Albums Podcasts More erties Categories Top 25 oer Fed
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,