2008年10月6日月曜日

【Objective-C/Cocoa/iPhone】iPhone SDK+Interface Builderの基本:おおまかな流れ

xcode 3.1.1, Window-Based Application

メモ。

プロジェクト名をMyAppとする。

追記:[Classes]を選択する。
[New File]から[Cocoa Touch Classes] [UIViewController subclass]を作成する。

MyViewControllerなどと名前を付ける。

MyAppDelegate.hに
@class MyViewController;

を追加する。インスタンス変数とアクセッサメソッドを追加する。
MyViewController *myViewController;

@property (nonatomic, retain) MyViewController *myViewController;


MyAppDelegate.mに追加。

#import "MyViewController.h"
...
@synthesize myViewController;
...



MyViewController *vc = [[MyViewController alloc] initWithNibName:@"ControllerView" bundle:[NSBundle mainBundle]];
self.myViewController = vc;
[vc release];

[window addSubview:[myViewController view]];

追記:addSubViewは間違い

deallocに追加

[myViewController release];


Interface Builderを起動する。

[New File]から[Cocoa Touch] [View]を選ぶ。

プロジェクトにControllerViewとして保存する。initWithNibName:@"ControllerView"

[File's Owner]を選び、MyViewControllerを選ぶ。

[File's Owner]から[View]に線(Ctrl+左ドラッグ)を延ばす。viewを選択する。

[Library]から[View]にUIパーツを貼付けていく。

MyViewController.hを編集する。インスタンス変数追加。

UITextField *textField;
UILabel *label;
NSString *string;

アクセッサメソッドとイベントハンドラ追加。

@property (nonatomic, retain) IBOutlet UITextField *textField;
@property (nonatomic, retain) IBOutlet UILabel *label;
@property (nonatomic, copy) NSString *string;
- (IBAction)someAction:(id)sender;


[File's Owner]からUIパーツに線を引く。
イベント源から[File's Owner]に線を引く。

MyViewController.mに追加。

@synthesize textField;
@synthesize label;
@synthesize string;

- (IBAction)someAction:(id)sender {
self.string = textField.string;

NSString *s = [[NSString alloc] initWithFormat:@"Hello, %@", self.string];
label.text = s;
[s release];
}
...
- (void) dealloc {
[textField release]:
[label release];
[string release];
[super dealloc];
}

ラベル: , , , ,

0 件のコメント:

コメントを投稿

登録 コメントの投稿 [Atom]

この投稿へのリンク:

リンクを作成

<< ホーム