190 likes | 202 Views
EEC-492/693/793 iPhone Application Development. Lecture 11 Wenbing Zhao & Nigamanth Sridhar. Outline. Administrative From now on: review your app/progress at the end of each session Navigation Controller Assignments: The push and pop app Continue b uild ing the calculator app.
E N D
EEC-492/693/793iPhone Application Development Lecture 11 Wenbing Zhao & Nigamanth Sridhar EEC492/693/793 - iPhone Application Development
Outline Administrative From now on: review your app/progress at the end of each session Navigation Controller Assignments: The push and pop app Continue building the calculator app 12/20/2019 2 EEC492/693/793 - iPhone Application Development EEC492/693/793 - iPhone Application Development
Navigation Controller • The navigation controller (UINavigationController) can be used to manage a stack of view controllers • Stack: a data structure that manages a collection of objects • last in, first out / first in, last out • Push: add an object to a stack • Pop: remove an object from the stack • Navigation bar • Title of the top view controller • Left bar button to go to the previous view controller (pop) • Right bar button to allow editing etc. EEC492/693/793 - iPhone Application Development
Example App: Mail Top view controller’s title Previous view controller’s title Top view controller’s view Top view controller’s toolbar EEC492/693/793 - iPhone Application Development
How to Navigate • Push to add a view controller (to the UINavigationController’s stack) • - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated; • Pop to remove a view controller from the stack (the previous one will become the top view controller) • - (UIViewController *)popViewControllerAnimated:(BOOL)animated; • Set to change the entire stack of view controllers • - (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated; EEC492/693/793 - iPhone Application Development
Pushing Your First View Controller - (void)applicationDidFinishLaunching { // Create a navigation controller navController = [[UINavigationController alloc] init]; // Push the first view controller on the stack [navController pushViewController:firstViewController animated:NO]; // Add the navigation controller’s view to the window [window addSubview:navController.view]; …. } EEC492/693/793 - iPhone Application Development
Push from within a View Controller on the Stack due to User Actions - (void)someAction:(id)sender { // Potentially create another view controller UIViewController *viewController = ...; [self.navigationController pushViewController:viewController animated:YES]; } • Where on earth does navigationController come from?! • It is defined in UIViewController as a property • Since our view controller inherited from UIViewController, we do have it as well • navigationItem is another property declared in UIViewController EEC492/693/793 - iPhone Application Development
Customizing Navigation • UINavigationItem: Describes appearance of the navigation bar • Title 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 Left Bar Button Item Navigation Item View Controller Title View Right Bar Button Item EEC492/693/793 - iPhone Application Development
Displaying a Title • UIViewController already has a title property • @property(nonatomic,copy) NSString *title; • Navigation item inherits automatically • Previous view controller’s title is displayed in back button viewController.title = @“Detail”; EEC492/693/793 - iPhone Application Development
Left & Right Buttons • UIBarButtonItem • 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) EEC492/693/793 - iPhone Application Development
Text Bar Button Item - (void)viewDidLoad { UIBarButtonItem *fooButton = [[UIBarButtonItem alloc] initWithTitle:@"Foo” style:UIBarButtonItemStyleBordered target:self action:@selector(foo:)]; self.navigationItem.leftBarButtonItem = fooButton; [fooButton release]; } EEC492/693/793 - iPhone Application Development
System Bar Button Item - (void)viewDidLoad { UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add:)]; self.navigationItem.rightBarButtonItem = addButton; [addButton release]; } EEC492/693/793 - iPhone Application Development
Edit/Done Button • Very common pattern • Every view controller has one available • Target/action already set up self.navigationItem.leftBarButtonItem = self.editButtonItem; // Called when the user toggles the edit/done button - (void)setEditing:(BOOL)editing animated:(BOOL)animated { // Update appearance of views } EEC492/693/793 - iPhone Application Development
Custom Title View • Arbitrary view in place of the title UISegmentedControl *segmentedControl = ... self.navigationItem.titleView = segmentedControl; [segmentedControl release]; EEC492/693/793 - iPhone Application Development
Back Button • If you don’t like the default back button (using the title of the view controller), you can customize it self.title = @“Hello there, CS193P!”; UIBarButtonItem *heyButton = [[UIBarButtonItem alloc] initWithTitle:@“Hey!” ...]; self.navigationItem.backButtonItem = heyButton; [heyButton release]; EEC492/693/793 - iPhone Application Development
Combining Tab Bar and Navigation Controllers • You can combine the tab bar and navigation controllers to create multiple parallel hierarchies EEC492/693/793 - iPhone Application Development
Tab Bar + Navigation Controllers View Controller Navigation Item View Controller Navigation Item View Controller View Controller View Controller Navigation Item EEC492/693/793 - iPhone Application Development
Nesting Navigation Controllers (into Tab Bar Controller) • Create a tab bar controller tabBarController = [[UITabBarController alloc] init]; • Create each navigation controller navController = [[UINavigationController alloc] init]; [navController pushViewController:firstViewController animated:NO]; • Add them to the tab bar controller tabBarController.viewControllers = [NSArray arrayWithObjects: navController, anotherNavController, someViewController, nil]; EEC492/693/793 - iPhone Application Development
Assignment: Push and Pop App EEC492/693/793 - iPhone Application Development