FrontPage > Apple > iOS開発メモ > XcodeとIBの連携

「Xcode」と「Interface Builder(IB)」の連携

IBとXcodeの連携は、「View Controller」クラスを使用します。

「IBOutlet (アウトレット)」と「IBAction (アクション)」

IBとXcodeのデータの設定・イベント処理等の連携は、
「Controller」クラスのヘッダ(xxxx.h)に「IBOutlet」と「IBAction」を事前に宣言する必要があります。

新規テンプレートで「View-based Application」の場合は、 「xxxx.xibファイル」(画面イメージ)だけでなく
「Delegate」「Controller」の「モジュール・ファイル(xxxx.m)」と「ヘッダ・ファイル(xxxx.h)」もテンプレートとして提供されます。

「Window-based Application」テンプレートは、「ViewController」ファイル等を各自で作成する必要があります。~

IBOutlet (アウトレット)

ヘッダの「@interface」内で宣言されたメンバ変数をIBで参照出来る様に「IBOutlet」として指定します。

外部から参照可能なメンバ変数 = アウトレット(outlet) → IBから参照できるメンバ変数(IBOutlet)

IBAction (アクション)

ヘッダ(xxxx.h)で「プロトタイプ宣言」と、クラス(xxxx.m)に「メソッド」を記述します。

先頭の「-」インスタンス・メソッド
先頭の「+」クラス・メソッド

View-based Application

※プロジェクト名「xxxx」として説明。適当に置き換えてください。

xxxxViewController.h

#import <UIKit/UIKit.h>

@interface xxxxViewController : UIViewController {
	IBOutlet	UITextField	*txtWait;
	IBOutlet	UITextField	*txtLength;
	IBOutlet	UIButton	*btnCalc;

	IBOutlet	UITextField	*txtBMI;
	IBOutlet	UITextField	*txtDefWait;

	IBOutlet	UILabel		*lblMsg;
}

- (IBAction) selectCalcCB:(id)sender;
@end

xxxxViewController.m

#import "xxxxViewController.h"

@implementation xxxxViewController

- (void)selectCalcCB:(id)sender {

	double		dWait = txtWait.text.doubleValue;
	double		dLength = txtLength.text.doubleValue / 100;
 	double		dBMI = dWait / (dLength*dLength);
	double		dDefWait = (dLength*dLength) * 22;

       // メッセージ初期化
	lblMsg.text = @"";

	if (isnan (dBMI)) {
		
		return;
	}

       // BMI表示 
	txtBMI.text = [NSString stringWithFormat:@"%.2f", dBMI];
	txtDefWait.text = [NSString stringWithFormat:@"%.2f", dDefWait];

       // メッセージ表示
	double		dCalc = dBMI - 25;
 	if ((dCalc >= 0.0) && (dCalc <5.0)) {
		lblMsg.text = @"肥満度1度";
	} else if ((dCalc >= 5.0) && (dCalc < 10.0)) {
		lblMsg.text = @"肥満度2度";
	} else if ((dCalc >= 10.0) && (dCalc < 15.0)) {
		lblMsg.text = @"肥満度3度";
	} else if (dCalc >= 15.0) {
		lblMsg.text = @"肥満度4度";
	}
}

// ...

IBで部品を配置

「IBOutlet」で指定した部品と同じ部品を配置。

#ref(): File not found: "view.png" at page "Xcode-IB"

IBの「Connections」を表示

「xxxxViewController.xib」の「File's Owner」を選択。

#ref(): File not found: "xib.png" at page "Xcode-IB"

見つからない場合は、[Tools]メニューの[Reveal in Document Window]を選択。

#ref(): File not found: "Connections.png" at page "Xcode-IB"

[Tools]メニューの[Controller Inspector]を選択。

IBで関連付け(Outlets)

#ref(): File not found: "select.png" at page "Xcode-IB"

「Controller」に表示されている「Outlets」の「メンバ変数」の右側の「○」を配置した部品までドラッグすることで、
「メンバ変数」と「部品」の関連付けができる。

#ref(): File not found: "view2.png" at page "Xcode-IB"

IBで関連付け(Actions)

「IBで関連付け(Outlets)」同様、「Received Actions」の項目を実施。

#ref(): File not found: "action.png" at page "Xcode-IB"

対応させるアクションを選択する。

メモ


トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2010-08-04 (水) 12:52:29