UIViewControllerと画面遷移について

画面遷移についても忘れないようにメモ
TableViewからの画面遷移のコードはwebでよく見かけるけれど、
UIView単品で、addsubviewとRemoveSubviewとか使わずにNavigationControllerにpushするタイプの画面遷移のコードは見かけないなぁ。
というわけで、いろいろ試した結果動いたものを書いてみる。


画面を好きなだけ移動して、戻るボタンを押していくと
まるでブラウザの戻るボタンのように戻れることがわかる。
いわゆるスタックってやつです。



appDelegeteにて起点となるControllerを生成。
HellowWorldViewControllerをトップ画面として呼ぶ

//ヘッダ
#import <UIKit/UIKit.h>

@class HellowWorldViewController;

@interface HellowWorldAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
	UIViewController* rootController;
	HellowWorldViewController* viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property(nonatomic,retain)UIViewController* rootController;

@end

//HellowWorldAppDlegate.m
//実装部
#import "HellowWorldAppDelegate.h"
#import "HellowWorldViewController.h"

@implementation HellowWorldAppDelegate

@synthesize window;
@synthesize rootController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
  	//windowを自力で作成
	CGRect bounds =[[UIScreen mainScreen]bounds];
	window = [[UIWindow alloc] initWithFrame:bounds];
	
	//起点となるControllerを作成し、UINavigationControllerに追加
	rootController = [[UINavigationController alloc]initWithRootViewController:viewController];
	
	//windowにControllerのviewを追加
	[window addSubview:rootController.view];
	
	
    [window makeKeyAndVisible];
	
	return YES;
}


- (void)dealloc {

    [window release];
    [super dealloc];
}
@end

HellewWorldViewController.m
トップの表示。
ナビゲーションバーに画面遷移用のボタンを生成。ViewController1を呼び出す

#import "HellowWorldViewController.h"

@implementation HellowWorldViewController

-(void)viewDidLoad{
	[super viewDidLoad];
	
	
	//こんばんは、世界!ラベルを追加
	//背景は黒、文字は白で
	
	UILabel* label =[[[UILabel alloc]initWithFrame:self.view.bounds]autorelease];
	label.text = @"こんにちは、世界!";
	label.backgroundColor = [UIColor whiteColor];
	label.textColor = [UIColor blackColor];
	label.textAlignment = UITextAlignmentCenter;
	label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
	[self.view addSubview:label];
	
	
	//UIButton* bt1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	UIBarButtonItem* bt1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self
																		action:@selector(ch)];
	

	self.navigationItem.rightBarButtonItem = bt1;
	
}


-(void)ch{
	Class class = NSClassFromString(@"ViewController1");
	id viewController = [[[class alloc]init]autorelease];
	
	if(viewController){
		[self.navigationController pushViewController:viewController animated:YES];
	}
}


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

@end

ViewController1.m の実装。ここにも画面遷移を実装してる。

#import "ViewController1.h"


@implementation ViewController1

-(void)viewDidLoad{
	[super viewDidLoad];
	
	
	//こんばんは、世界!ラベルを追加
	//背景は黒、文字は白で
	
	UILabel* label =[[[UILabel alloc]initWithFrame:self.view.bounds]autorelease];
	label.text = @"こんばんは、世界!";
	label.backgroundColor = [UIColor blackColor];
	label.textColor = [UIColor whiteColor];
	label.textAlignment = UITextAlignmentCenter;
	label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
	[self.view addSubview:label];
	
	UIButton* bt1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[bt1 setTitle:@"go" forState:UIControlStateNormal];
	[bt1 sizeToFit];
	CGPoint newPoint = self.view.center;
	newPoint.y=30;
	newPoint.x = 50;
	bt1.center = newPoint;
	[bt1 addTarget:self action:@selector(ch)
  forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:bt1];
	
	
	
}

-(void)ch{
	Class class = NSClassFromString(@"HellowWorldViewController");
	id viewController = [[[class alloc]init]autorelease];
	
	if(viewController){
		[self.navigationController pushViewController:viewController animated:YES];
	}
}


-(id)init{
	if((self = [super init])){
		//tabBar用設定
		self.title = @"こんばんわ";

	}
	return self;
}

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

@end