cs193p - lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · cs193p...

101
CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers 1 Thursday, January 28, 2010

Upload: others

Post on 17-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

CS193P - Lecture 7iPhone Application Development

Navigation & Tab Bar Controllers

1Thursday, January 28, 2010

Page 2: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Announcements• Assignment 3 is due tomorrow• Paparazzi 1 is due on Wednesday February 3rd

2Thursday, January 28, 2010

Page 3: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Today’s Topics• Navigation Controllers

■ Application Data Flow

• Customizing Navigation• Tab Bar Controllers• Combining Approaches

3Thursday, January 28, 2010

Page 4: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Navigation Controllers

4Thursday, January 28, 2010

Page 5: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

UINavigationController• Stack of view controllers• Navigation bar

NavigationController

5Thursday, January 28, 2010

Page 6: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

UINavigationController• Stack of view controllers• Navigation bar

View Controller

View Controller

View Controller

NavigationController

5Thursday, January 28, 2010

Page 7: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

How It Fits Together

6Thursday, January 28, 2010

Page 8: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

How It Fits Together• Top view controller’s view

6Thursday, January 28, 2010

Page 9: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

How It Fits Together• Top view controller’s view• Top view controller’s title

6Thursday, January 28, 2010

Page 10: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

How It Fits Together• Top view controller’s view• Top view controller’s title• Previous view controller’s title

6Thursday, January 28, 2010

Page 11: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

How It Fits Together• Top view controller’s view• Top view controller’s title• Previous view controller’s title• Top view controller’s toolbar

items (iPhone OS 3.0)

6Thursday, January 28, 2010

Page 12: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Modifying the Navigation Stack

7Thursday, January 28, 2010

Page 13: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Modifying the Navigation Stack• Push to add a view controller- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

7Thursday, January 28, 2010

Page 14: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Modifying the Navigation Stack• Push to add a view controller

• Pop to remove a view controller

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

- (UIViewController *)popViewControllerAnimated:(BOOL)animated;

7Thursday, January 28, 2010

Page 15: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Modifying the Navigation Stack• Push to add a view controller

• Pop to remove a view controller

• Set to change the entire stack of view controllers(iPhone OS 3.0)

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

- (UIViewController *)popViewControllerAnimated:(BOOL)animated;

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated;

7Thursday, January 28, 2010

Page 16: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Pushing Your First View Controller

8Thursday, January 28, 2010

Page 17: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Pushing Your First View Controller

- (void)applicationDidFinishLaunching// Create a navigation controllernavController = [[UINavigationController alloc] init];

}

8Thursday, January 28, 2010

Page 18: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Pushing Your First View Controller

- (void)applicationDidFinishLaunching// Create a navigation controllernavController = [[UINavigationController alloc] init];

}

// Push the first view controller on the stack[navController pushViewController:firstViewController

animated:NO];

8Thursday, January 28, 2010

Page 19: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Pushing Your First View Controller

- (void)applicationDidFinishLaunching// Create a navigation controllernavController = [[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];

8Thursday, January 28, 2010

Page 20: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

In Response to User Actions

9Thursday, January 28, 2010

Page 21: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

In Response to User Actions• Push from within a view controller on the stack- (void)someAction:(id)sender{

// Potentially create another view controllerUIViewController *viewController = ...;

[self.navigationController pushViewController:viewController animated:YES];

}

9Thursday, January 28, 2010

Page 22: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

In Response to User Actions• Push from within a view controller on the stack

• Almost never call pop directly!■ Automatically invoked by the back button

- (void)someAction:(id)sender{

// Potentially create another view controllerUIViewController *viewController = ...;

[self.navigationController pushViewController:viewController animated:YES];

}

9Thursday, January 28, 2010

Page 23: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Demo:Pushing & Popping

10Thursday, January 28, 2010

Page 24: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Application Data Flow

11Thursday, January 28, 2010

Page 25: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Paparazzi

12Thursday, January 28, 2010

Page 26: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

A Controller for Each Screen

List Controller

Detail Controller

List Controller

13Thursday, January 28, 2010

Page 27: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Connecting View Controllers

14Thursday, January 28, 2010

Page 28: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Connecting View Controllers• Multiple view controllers may need to share data

14Thursday, January 28, 2010

Page 29: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Connecting 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

14Thursday, January 28, 2010

Page 30: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

How 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

List Controller

Detail Controller

15Thursday, January 28, 2010

Page 31: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

How 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

List Controller

Detail Controller

Application Delegate

15Thursday, January 28, 2010

Page 32: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

How 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

List Controller

Detail Controller

Application Delegate

Don’t Do This!

15Thursday, January 28, 2010

Page 33: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Best Practices for Data Flow• Figure out exactly what needs to be communicated

List Controller

Detail Controller

16Thursday, January 28, 2010

Page 34: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Best Practices for Data Flow• Figure out exactly what needs to be communicated• Define input parameters for your view controller

List Controller

Detail Controller

16Thursday, January 28, 2010

Page 35: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Best Practices for Data Flow• Figure out exactly what needs to be communicated• Define input parameters for your view controller

List Controller

Detail Controller

Data

16Thursday, January 28, 2010

Page 36: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

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)

List Controller

Detail Controller

16Thursday, January 28, 2010

Page 37: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

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)

List Controller

Detail Controller

I care!

16Thursday, January 28, 2010

Page 38: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

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)

List Controller

Detail Controller

16Thursday, January 28, 2010

Page 39: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Example:UIImagePickerController

17Thursday, January 28, 2010

Page 40: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Demo:Passing Data Along

18Thursday, January 28, 2010

Page 41: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Customizing Navigation

19Thursday, January 28, 2010

Page 42: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Customizing Navigation• Buttons or custom controls• Interact with the entire screen

20Thursday, January 28, 2010

Page 43: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Customizing Navigation• Buttons or custom controls• Interact with the entire screen

20Thursday, January 28, 2010

Page 44: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

UINavigationItem• Describes appearance of the navigation bar

■ Title string or custom title view■ Left & right bar buttons■ More properties defined in UINavigationBar.h

21Thursday, January 28, 2010

Page 45: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

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

21Thursday, January 28, 2010

Page 46: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Navigation Item Ownership

View Controller

Right Bar Button Item

Navigation Item

Left Bar Button Item

Title View

22Thursday, January 28, 2010

Page 47: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

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

23Thursday, January 28, 2010

Page 48: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

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”;

23Thursday, January 28, 2010

Page 49: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

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)

24Thursday, January 28, 2010

Page 50: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Text Bar Button Item

25Thursday, January 28, 2010

Page 51: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Text Bar Button Item

25Thursday, January 28, 2010

Page 52: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Text Bar Button Item

- (void)viewDidLoad{

UIBarButtonItem *fooButton = [[UIBarButtonItem alloc]initWithTitle:@"Foo”style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(foo:)];

self.navigationItem.leftBarButtonItem = fooButton;

[fooButton release];}

25Thursday, January 28, 2010

Page 53: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

System Bar Button Item

26Thursday, January 28, 2010

Page 54: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

System Bar Button Item

26Thursday, January 28, 2010

Page 55: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

System Bar Button Item

- (void)viewDidLoad{

UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAddstyle:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(add:)];

self.navigationItem.rightBarButtonItem = addButton;

[addButton release];}

26Thursday, January 28, 2010

Page 56: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

• Very common pattern• Every view controller has one available

■ Target/action already set up

Edit/Done Button

27Thursday, January 28, 2010

Page 57: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

• Very common pattern• Every view controller has one available

■ Target/action already set up

Edit/Done Button

27Thursday, January 28, 2010

Page 58: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

• Very common pattern• Every view controller has one available

■ Target/action already set up

Edit/Done Button

self.navigationItem.leftBarButtonItem = self.editButtonItem;

27Thursday, January 28, 2010

Page 59: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

• Very common pattern• Every view controller has one available

■ Target/action already set up

Edit/Done Button

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}

27Thursday, January 28, 2010

Page 60: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Custom Title View• Arbitrary view in place of the title

28Thursday, January 28, 2010

Page 61: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Custom Title View• Arbitrary view in place of the title

28Thursday, January 28, 2010

Page 62: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Custom Title View• Arbitrary view in place of the title

UISegmentedControl *segmentedControl = ...self.navigationItem.titleView = segmentedControl;[segmentedControl release];

28Thursday, January 28, 2010

Page 63: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

• Sometimes a shorter back button is needed

Back Button

29Thursday, January 28, 2010

Page 64: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

• Sometimes a shorter back button is needed

Back Button

29Thursday, January 28, 2010

Page 65: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

• Sometimes a shorter back button is needed

Back Button

self.title = @“Hello there, CS193P!”;

29Thursday, January 28, 2010

Page 66: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

• Sometimes a shorter back button is needed

Back Button

self.title = @“Hello there, CS193P!”;

UIBarButtonItem *heyButton = [[UIBarButtonItem alloc] initWithTitle:@“Hey!” ...];self.navigationItem.backButtonItem = heyButton;

[heyButton release];

29Thursday, January 28, 2010

Page 67: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

• Sometimes a shorter back button is needed

Back Button

self.title = @“Hello there, CS193P!”;

UIBarButtonItem *heyButton = [[UIBarButtonItem alloc] initWithTitle:@“Hey!” ...];self.navigationItem.backButtonItem = heyButton;

[heyButton release];

29Thursday, January 28, 2010

Page 68: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Demo:Customizing Buttons

30Thursday, January 28, 2010

Page 69: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Tab Bar Controllers

31Thursday, January 28, 2010

Page 70: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

UITabBarController• Array of view controllers• Tab bar

Tab BarController

32Thursday, January 28, 2010

Page 71: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

UITabBarController• Array of view controllers• Tab bar

View Controller

View Controller

View Controller

Tab BarController

32Thursday, January 28, 2010

Page 72: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

How It Fits Together

33Thursday, January 28, 2010

Page 73: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

How It Fits Together• Selected view controller’s view

33Thursday, January 28, 2010

Page 74: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

How It Fits Together• Selected view controller’s view• All view controllers’ titles

33Thursday, January 28, 2010

Page 75: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Setting Up a Tab Bar Controller

34Thursday, January 28, 2010

Page 76: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Setting Up a Tab Bar Controller

- (void)applicationDidFinishLaunching// Create a tab bar controllertabBarController = [[UITabBarController alloc] init];

}

34Thursday, January 28, 2010

Page 77: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Setting Up a Tab Bar Controller

- (void)applicationDidFinishLaunching// Create a tab bar controllertabBarController = [[UITabBarController alloc] init];

}

// Set the array of view controllerstabBarController.viewControllers = myViewControllers;

34Thursday, January 28, 2010

Page 78: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Setting Up a Tab Bar Controller

- (void)applicationDidFinishLaunching// Create a tab bar controllertabBarController = [[UITabBarController alloc] init];

}

// Set the array of view controllerstabBarController.viewControllers = myViewControllers;

// Add the tab bar controller’s view to the window[window addSubview:tabBarController.view];

34Thursday, January 28, 2010

Page 79: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Tab Bar Appearance• View controllers can define their appearance in the tab bar

35Thursday, January 28, 2010

Page 80: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Tab Bar Appearance• View controllers can define their appearance in the tab bar

• UITabBarItem■ Title + image or system item

35Thursday, January 28, 2010

Page 81: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Tab Bar Appearance• View controllers can define their appearance in the tab bar

• UITabBarItem■ Title + image or system item

• Each view controller comes with a tab bar item for customizing

35Thursday, January 28, 2010

Page 82: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Creating Tab Bar Items• Title and image

36Thursday, January 28, 2010

Page 83: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Creating Tab Bar Items• Title and image

36Thursday, January 28, 2010

Page 84: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Creating Tab Bar Items• Title and image

- (void)viewDidLoad{

UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@“Playlists” image:[UIImage imageNamed:@“music.png”] tag:0];self.tabBarItem = item;[item release];

}

36Thursday, January 28, 2010

Page 85: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Creating Tab Bar Items• System item

37Thursday, January 28, 2010

Page 86: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Creating Tab Bar Items• System item

37Thursday, January 28, 2010

Page 87: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Creating Tab Bar Items• System item

- (void)viewDidLoad{

UITabBarItem *item = [[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemBookmarks tag:0]self.tabBarItem = item;[item release];

}

37Thursday, January 28, 2010

Page 88: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Demo:Using a Tab Bar Controller

38Thursday, January 28, 2010

Page 89: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

More View Controllers• What happens when a tab bar controller has too many

view controllers to display at once?

39Thursday, January 28, 2010

Page 90: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

More View Controllers• What happens when a tab bar controller has too many

view controllers to display at once?■ “More” tab bar item

displayed automatically

39Thursday, January 28, 2010

Page 91: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

More 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

remaining view controllers

39Thursday, January 28, 2010

Page 92: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

More 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

remaining view controllers■ Customize order

39Thursday, January 28, 2010

Page 93: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Combining Approaches

40Thursday, January 28, 2010

Page 94: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Tab Bar + Navigation ControllersMultiple parallel hierarchies

41Thursday, January 28, 2010

Page 95: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Tab Bar + Navigation Controllers

Tab Bar Controller

42Thursday, January 28, 2010

Page 96: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Tab Bar + Navigation Controllers

Navigation Controller

View Controller

Navigation Controller

View Controller

View ControllerTab Bar Controller

42Thursday, January 28, 2010

Page 97: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Nesting Navigation Controllers

43Thursday, January 28, 2010

Page 98: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Nesting Navigation Controllers• Create a tab bar controllertabBarController = [[UITabBarController alloc] init];

43Thursday, January 28, 2010

Page 99: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Nesting Navigation Controllers• Create a tab bar controller

• Create each navigation controllernavController = [[UINavigationController alloc] init];[navController pushViewController:firstViewController animated:NO];

tabBarController = [[UITabBarController alloc] init];

43Thursday, January 28, 2010

Page 100: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Nesting Navigation Controllers• Create a tab bar controller

• Create each navigation controller

• Add them to the tab bar controller

navController = [[UINavigationController alloc] init];[navController pushViewController:firstViewController animated:NO];

tabBarController = [[UITabBarController alloc] init];

tabBarController.viewControllers = [NSArray arrayWithObjects: navController, anotherNavController, someViewController, nil];

43Thursday, January 28, 2010

Page 101: CS193P - Lecture 7web.stanford.edu/class/cs193p/cgi-bin/drupal/system/... · 2020. 9. 8. · CS193P - Lecture 7 iPhone Application Development Navigation & Tab Bar Controllers Thursday,

Questions?

44Thursday, January 28, 2010