ViewControllerを使って表示してみる
前回はUIViewを作成して表示してみましたが、
今回はViewControllerを使って"Hello World"してみたいなぁと思います
今回もお世話になる本はこれです。
iPhone SDK Application Development: Building Applications for the AppStore
- 作者: Jonathan Zdziarski
- 出版社/メーカー: O'Reilly Media
- 発売日: 2009/02/06
- メディア: ペーパーバック
- 購入: 2人 クリック: 110回
- この商品を含むブログ (14件) を見る
これのp.54にあるソースを写経します。
ControllerDemoという名前で「Windows-Based Application」で作成します。
そこでできたプロジェクト内のフォルダ「Classes」を選択し
「ファイル」→「新規ファイル」→「UIviewController subClass」を選択し、ControllerDemoViewControllerと名前を付けて保存します。
作成されたファイルに対するソースはこれです。
ControllerDemoViewController.h
#import <UIKit/UIKit.h> @interface ControllerDemoViewController : UIViewController { NSString *helloWorld, *woahDizzy; UITextView *textView; } @end
ControllerDemoViewController.m
#import "ControllerDemoViewController.h" @implementation ControllerDemoViewController - (id)init { self = [super init]; if(self != nil){ helloWorld = [[NSString alloc] initWithString:@"Hello World!"]; woahDizzy = [[NSString alloc] initWithString:@"Woah, I'm Dizzy!"]; } return self; } - (void)loadView { [super loadView]; textView = [[UITextView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; textView.text = helloWorld; self.view = textView; } // 横向けた時に反応するかどうか。今回は反応させるためにYES - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return YES; } // 横にした時にwoahDizzyの文言を放り込む - (void)didRotateFromInterfaceOrientation: (UIInterfaceOrientation)fromInterfaceOrientation{ textView.text = woahDizzy; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview // Release anything that's not essential, such as cached data } - (void)dealloc { [helloWorld release]; [woahDizzy release]; [textView release]; [super dealloc]; } @end
肝心のControllerDemoAppDelegateはこの通りです。
ControllerDemoAppDelegate.h
#import <UIKit/UIKit.h> @class ControllerDemoViewController; @interface ControllerDemoAppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; ControllerDemoViewController *viewController; } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet ControllerDemoViewController *viewController; @end
ControllerDemoAppDelegateクラスの中身はこんな感じです。
ControllerDemoAppDelegate.m
#import "ControllerDemoAppDelegate.h" #import "ControllerDemoViewController.h" @implementation ControllerDemoAppDelegate @synthesize window; @synthesize viewController; - (void)applicationDidFinishLaunching:(UIApplication *)application { CGRect screenBounds = [[UIScreen mainScreen] bounds]; self.window = [[[UIWindow alloc] initWithFrame:screenBounds] autorelease]; viewController = [[ControllerDemoViewController alloc] init]; // Override point for customization after application launch [window addSubview:viewController.view]; [window makeKeyAndVisible]; } - (void)dealloc { [viewController release]; [window release]; [super dealloc]; } @end
駆け足ですが、ビルドしてシミュレータで動かしてみるとこんな感じになるはずです。
横向けにした時には文言が変わりますね。