아이폰 앱 개발 팁(13) : Head First iPhone Development #4
Chapter 4. multiple views - A table with a view
The title of the main viewDisplay drink list
- Declare the drink array. (.h file)
- NSMutableArray* drinks;
- @property ~
- @synthesize ~
- Implement and populate the array. (viewDidLoad method at .m file)
- NSMutableArray* tmpArray = [[NSMutableArray alloc] initWithObjects:@"Firecracker", @"Lemon Drop", @"Mojito", nil];
- Tell the table how many rows you have. (numberOfRowsInSection method at .m file)
- return [self.drinks count];
- Populate the table cells. (cellForRowAtIndexPath method at .m file)
- cell.textLabel.text = [self.drinks objectAtIndex:indexPath.row];
Variable allocation
// 1st Method : Book
NSMutableArray* tmpArray = [[NSMutableArray alloc] initWithObjects:@"Firecracker", @"Lemon Drop", @"Mojito", nil];
self.drinks = tmpArray;
[tmpArray release];
// 2nd Method : MissFlash
self.drinks = [[NSMutableArray alloc] initWithObjects:@"Firecracker", @"Lemon Drop", @"Mojito", nil];
// 3rd Method : Book
NSString* path = [[NSBundle mainBundle] pathForResource:@"DrinkArray" ofType:@"plist"];
NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
self.drinks = tmpArray;
[tmpArray release];
Use the navigation controller to switch between views
- A view stack for moving between views
- A navigation bar for buttons and a title
- A navigation toolbar for view-specific buttons
Navigation controllers maintain a stack of views
- Use the tap notification in the table view delegate
- tableview:didSelectRowAtIndexPath
Instantiate a view controller like any other class
- [[DrinkDetailViewController alloc] initWithNibName:@"DrinkDetailViewController" bundle:nil];
- At RootViewController.m
#import "DrinkDetailViewController.h"
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DrinkDetailViewController *drinkDetailViewController = [[DrinkDetailViewController alloc] initWithNibName:@"DrinkDetailViewController" bundle:nil];
[self.navigationController pushViewController:drinkDetailViewController animated:YES];
[drinkDetailViewController release];
}
Create a back button
Debug process
- First stop on your debugging adventure: the console (Run -> Console)
- Interact with your application while it's running
- And when it's about to stop running
- Xcode supports you after your app breaks, too
- The Xcode debugger shows you the state of your application (Shift-Commander-Y key)
![]() Steve Jobs for Fortune magazine by tsevis ![]() ![]() ![]() |