2011年6月4日土曜日

Xcode tableViewで画面を切り替える





DetailViewController.h
#import <UIKit/UIKit.h>


@interface DetailViewController : UIViewController {

 IBOutlet UILabel *label;
}

@property(nonatomic, retain) UILabel *label;

@end

DetailViewController.m
#import "DetailViewController.h"


@implementation DetailViewController

@synthesize label;

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

tableViewAppDelegate.m
#import "tableViewAppDelegate.h"
#import "RootViewController.h"


@implementation tableViewAppDelegate

@synthesize window;
@synthesize navigationController;

-(void)applicationDidFinishLaunching:(UIApplication *)application{
 [window makeKeyAndVisible];
 [window addSubview:[RootViewController view]];
}

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];

    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {

}

- (void)applicationDidEnterBackground:(UIApplication *)application {
 
}

- (void)applicationWillEnterForeground:(UIApplication *)application {

}


- (void)applicationDidBecomeActive:(UIApplication *)application {
 
}

- (void)applicationWillTerminate:(UIApplication *)application {

}


#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {

}


- (void)dealloc {
 [navigationController release];
 [window release];
 [super dealloc];
}


@end

RootViewController.h
#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController {
 
 NSArray *dataArray;
}

@property(nonatomic, retain) NSArray *dataArray;

@end


RootViewController.m
#import "RootViewController.h"
#import "DetailViewController.h"

@implementation RootViewController

@synthesize dataArray;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
 self.dataArray = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];
}

#pragma mark -
#pragma mark Table view data source

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [dataArray count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
 // Configure the cell.
 cell.textLabel.text = [NSString stringWithFormat:@"%@", [dataArray objectAtIndex:indexPath.row]];
    return cell;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
 
  DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" 
                       bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
  [self.navigationController pushViewController:detailViewController animated:YES];
 
 [[detailViewController label] setText:[NSString stringWithFormat:@"%@", [dataArray objectAtIndex:indexPath.row]]];
  [detailViewController release];
  
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end


参考


YouTube - xcode programming tutorial#4 part 1/3: UITableView and UINavigationController
http://www.youtube.com/watch?v=T8YdIqBjpY4&feature=related&hd=1

YouTube - xcode programming tutorial#4 part 2/3: UITableView and UINavigationController
http://www.youtube.com/watch?v=2VA6uTfJcnc&feature=related&hd=1

YouTube - xcode programming tutorial#4 part 3/3: UITableView and UINavigationController
http://www.youtube.com/watch?v=hK9i7maWQC8&feature=related&hd=1





0 コメント: