Navigation IAP 2010 iphonedev.csail.mit.edu edward benson / - - PowerPoint PPT Presentation

navigation
SMART_READER_LITE
LIVE PREVIEW

Navigation IAP 2010 iphonedev.csail.mit.edu edward benson / - - PowerPoint PPT Presentation

Navigation IAP 2010 iphonedev.csail.mit.edu edward benson / eob@csail.mit.edu Wednesday, January 13, 2010 Today Programmatically Created Views Tab Bar Controllers Modal View Controllers & Tables Navigation


slide-1
SLIDE 1

Navigation

IAP 2010 ❄ edward benson / eob@csail.mit.edu iphonedev.csail.mit.edu

Wednesday, January 13, 2010

slide-2
SLIDE 2

Today

  • Programmatically Created Views
  • Tab Bar Controllers
  • Modal View Controllers &
  • Tables
  • Navigation Controllers

Wednesday, January 13, 2010

slide-3
SLIDE 3

How many of you would pay 99c for this?

Rock!

Player 2 Wins!

Paper!

Wednesday, January 13, 2010

slide-4
SLIDE 4

How many of you would pay 99c for this?

Wednesday, January 13, 2010

slide-5
SLIDE 5

How many of you would pay 99c for this?

Wednesday, January 13, 2010

slide-6
SLIDE 6

Interface is important.

Wednesday, January 13, 2010

slide-7
SLIDE 7

Programmatically Created Views

Wednesday, January 13, 2010

slide-8
SLIDE 8

Name it AboutController

Wednesday, January 13, 2010

slide-9
SLIDE 9

/* // Implement loadView to create a view // hierarchy programmatically, without using a nib.

  • (void)loadView {

} */

Wednesday, January 13, 2010

slide-10
SLIDE 10

Wednesday, January 13, 2010

slide-11
SLIDE 11
  • (void)loadView {

NSURL *url = [NSURL URLWithString:@"http://www.google.com"]; }

Wednesday, January 13, 2010

slide-12
SLIDE 12
  • (void)loadView {

NSURL *url = [NSURL URLWithString:@"http://www.google.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; }

Wednesday, January 13, 2010

slide-13
SLIDE 13
  • (void)loadView {

NSURL *url = [NSURL URLWithString:@"http://www.google.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; UIWebView *webView = [[UIWebView alloc] init]; }

Wednesday, January 13, 2010

slide-14
SLIDE 14
  • (void)loadView {

NSURL *url = [NSURL URLWithString:@"http://www.google.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; UIWebView *webView = [[UIWebView alloc] init]; [webView loadRequest:request]; }

Wednesday, January 13, 2010

slide-15
SLIDE 15
  • (void)loadView {

NSURL *url = [NSURL URLWithString:@"http://www.google.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; UIWebView *webView = [[UIWebView alloc] init]; [webView loadRequest:request]; self.view = webView; }

Wednesday, January 13, 2010

slide-16
SLIDE 16
  • (void)loadView {

NSURL *url = [NSURL URLWithString:@"http://www.google.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; UIWebView *webView = [[UIWebView alloc] init]; [webView loadRequest:request]; self.view = webView; [webView release]; }

Wednesday, January 13, 2010

slide-17
SLIDE 17

Modal Views (How do we show it?)

Wednesday, January 13, 2010

slide-18
SLIDE 18

Modal View Controllers (we interrupt this message to bring you...)

Wednesday, January 13, 2010

slide-19
SLIDE 19
  • (IBAction)aboutClicked:(id)sender;

RPSGameViewController.h RPSGameViewController.m

  • (IBAction)aboutClicked:(id)sender {

AboutController *ac = [[[AboutController alloc] init] autorelease]; [self presentModalViewController:ac animated:YES];

}

Now let’s wire it up in IB.. Ex3-1.zip

Wednesday, January 13, 2010

slide-20
SLIDE 20

So how do we get rid of the modal view? .. (in a minute)

Wednesday, January 13, 2010

slide-21
SLIDE 21

Navigation Controllers

Wednesday, January 13, 2010

slide-22
SLIDE 22

Navigation Controllers (Push, Pop views)

Wednesday, January 13, 2010

slide-23
SLIDE 23
  • (IBAction)aboutClicked:(id)sender {

AboutController *ac = [[[AboutController alloc] init] autorelease]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:ac]; [self presentModalViewController:nav animated:YES]; [nav release]; }

RPSGameViewController.m

Wednesday, January 13, 2010

slide-24
SLIDE 24
  • (id)init {

if (self = [super init]) { self.title = @"About"; } return self; }

AboutController.m

Wednesday, January 13, 2010

slide-25
SLIDE 25

AboutController.m

  • (id)init {

if (self = [super init]) { self.title = @"About"; UIBarButtonItem *dismiss = [[UIBarButtonItem alloc] init]; } return self; }

Wednesday, January 13, 2010

slide-26
SLIDE 26

AboutController.m

  • (id)init {

if (self = [super init]) { self.title = @"About"; UIBarButtonItem *dismiss = [[UIBarButtonItem alloc] init]; dismiss.title = @"Dismiss"; } return self; }

Wednesday, January 13, 2010

slide-27
SLIDE 27

AboutController.m

  • (id)init {

if (self = [super init]) { self.title = @"About"; UIBarButtonItem *dismiss = [[UIBarButtonItem alloc] init]; dismiss.title = @"Dismiss"; dismiss.target = self; } return self; }

Wednesday, January 13, 2010

slide-28
SLIDE 28

AboutController.m

  • (id)init {

if (self = [super init]) { self.title = @"About"; UIBarButtonItem *dismiss = [[UIBarButtonItem alloc] init]; dismiss.title = @"Dismiss"; dismiss.target = self; dismiss.action = @selector(dismissClicked); } return self; }

Wednesday, January 13, 2010

slide-29
SLIDE 29

AboutController.m

  • (id)init {

if (self = [super init]) { self.title = @"About"; UIBarButtonItem *dismiss = [[UIBarButtonItem alloc] init]; dismiss.title = @"Dismiss"; dismiss.target = self; dismiss.action = @selector(dismissClicked); self.navigationItem.leftBarButtonItem = dismiss; } return self; }

Why does it crash?

Wednesday, January 13, 2010

slide-30
SLIDE 30
  • (void)dismissClicked {

[self dismissModalViewControllerAnimated:YES]; }

AboutController.m What happens if we don’t put this in the .h file? Ex3-2.zip

Wednesday, January 13, 2010

slide-31
SLIDE 31

Navigation Controllers We misused the NavigationController here. It was just an easy way to get the Navigation Bar . We’ll revisit them in a bit to see how they are really used.

Wednesday, January 13, 2010

slide-32
SLIDE 32

Tab Bar Controllers

Wednesday, January 13, 2010

slide-33
SLIDE 33

Tab Bars (Multiplex between views)

UITabBarController

Wednesday, January 13, 2010

slide-34
SLIDE 34

“Wrapping Controllers” We used a Navigation Controller by wrapping it around another controller

[[UINavigationController alloc] initWithRootViewController:ac];

It knew what title to show because of the title property

  • n its child controller.

Wednesday, January 13, 2010

slide-35
SLIDE 35

Remember our app delegate

  • (void)applicationDidFinishLaunching:(UIApplication *)application

{ // Override point for customization after app launch gameViewController = [[RPSGameViewController alloc] init]; [window addSubview:gameViewController.view]; [window makeKeyAndVisible]; }

To use a tab bar controller, we just “wrap” controllers in a similar way.

Wednesday, January 13, 2010

slide-36
SLIDE 36

viewControllers

An array of the root view controllers displayed by the tab bar interface.

@property(nonatomic, copy) NSArray *viewControllers

Wednesday, January 13, 2010

slide-37
SLIDE 37
  • (void)applicationDidFinishLaunching:(UIApplication *)application {

// Override point for customization after app launch

  • gameViewController = [[RPSGameViewController alloc] init];
  • AboutController *a1 = [[[AboutController alloc] init] autorelease];
  • AboutController *a2 = [[[AboutController alloc] init] autorelease];
  • AboutController *a3 = [[[AboutController alloc] init] autorelease];
  • UITabBarController *tabs = [[UITabBarController alloc] init];
  • tabs.viewControllers = [NSArray arrayWithObjects:gameViewController, a1, a2, a3, nil];
  • [window addSubview:tabs.view];
  • [window makeKeyAndVisible];

}

In the App Delegate Create three AboutController instances

#import "AboutController.h"

<- Remember, at the top

Wednesday, January 13, 2010

slide-38
SLIDE 38
  • (void)applicationDidFinishLaunching:(UIApplication *)application {

// Override point for customization after app launch

  • gameViewController = [[RPSGameViewController alloc] init];
  • AboutController *a1 = [[[AboutController alloc] init] autorelease];
  • AboutController *a2 = [[[AboutController alloc] init] autorelease];
  • AboutController *a3 = [[[AboutController alloc] init] autorelease];
  • UITabBarController *tabs = [[UITabBarController alloc] init];
  • tabs.viewControllers = [NSArray arrayWithObjects:gameViewController, a1, a2, a3, nil];
  • [window addSubview:tabs.view];
  • [window makeKeyAndVisible];

}

In the App Delegate

Wednesday, January 13, 2010

slide-39
SLIDE 39
  • (void)applicationDidFinishLaunching:(UIApplication *)application {

// Override point for customization after app launch

  • gameViewController = [[RPSGameViewController alloc] init];
  • AboutController *a1 = [[[AboutController alloc] init] autorelease];
  • AboutController *a2 = [[[AboutController alloc] init] autorelease];
  • AboutController *a3 = [[[AboutController alloc] init] autorelease];
  • UITabBarController *tabs = [[UITabBarController alloc] init];
  • tabs.viewControllers = [NSArray arrayWithObjects:gameViewController, a1, a2, a3, nil];
  • [window addSubview:tabs.view];
  • [window makeKeyAndVisible];

}

In the App Delegate

Wednesday, January 13, 2010

slide-40
SLIDE 40

Wednesday, January 13, 2010

slide-41
SLIDE 41

Tables

Wednesday, January 13, 2010

slide-42
SLIDE 42

Tables (Show lists)

UITableViewController

Wednesday, January 13, 2010

slide-43
SLIDE 43

Call it GameHistory Controller

Wednesday, January 13, 2010

slide-44
SLIDE 44

In the App Delegate

  • (void)applicationDidFinishLaunching:(UIApplication *)application {

// Override point for customization after app launch

  • gameViewController = [[RPSGameViewController alloc] init];
  • GameHistoryController *history = [[[GameHistoryController alloc] init] autorelease];
  • UINavigationController *historyWrapper = [[[UINavigationController alloc]

initWithRootViewController:history] autorelease];

  • UITabBarController *tabs = [[UITabBarController alloc] init];
  • tabs.viewControllers = [NSArray arrayWithObjects:gameViewController,

historyWrapper, nil];

  • [window addSubview:tabs.view];
  • [window makeKeyAndVisible];

}

#import "GameHistoryController.h" <- Remember, at the top

Remove the AboutController instances, Create a GameHistoryController Wrap it in a NavigationController Ex3-4.zip

Wednesday, January 13, 2010

slide-45
SLIDE 45

Looking at a Table Delegate

UITableViewDataSource Protocol UITableViewDelegate Protocol UITableViewController Class

Methods that inform the table about its contents Methods related to user action on the table and table style manipulation A starting base for you

Wednesday, January 13, 2010

slide-46
SLIDE 46

// Customize the number of rows in the table view.

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return 20; }

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  • ....

// Set up the cell... cell.text = @"TEST";

Wednesday, January 13, 2010

slide-47
SLIDE 47
  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

// Navigation logic may go here. Create and push another view controller. GameHistoryController *g = [[GameHistoryController alloc] init]; [self.navigationController pushViewController:g animated:YES]; [g release];

Ex3-5.zip

Wednesday, January 13, 2010

slide-48
SLIDE 48

(demo of other ways to manipulate the table)

Wednesday, January 13, 2010