(カスタムUIViewControllerサブクラス内のコードについて話しています-ちなみに、IBは使用していません)わかりました。self.viewメンバーを-(void)loadViewに設定してから、コントロールやビューなどを作成します。 in-(void)viewDidLoadをクリックし、サブビューに追加します。コントロールがメンバーでない場合、それを作成してメソッドでローカルにリリースすると、次のようになります:( UILabelを使用)
- (void)viewDidLoad {
UILabel *localLabel = [[UILabel alloc] initWithFrame:CGRectMake(81, 384, 148, 21)];
localLabel.text = @"I'm a Label!";
localLabel.AutoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleBottomMargin);
[self.view addSubview:localLabel];
[localLabel release];
[super viewDidLoad];
}
これは、ローカルでラベルを作成し、そのプロパティを設定し、サブビューに追加してリリースする方法のほんの一例です。しかし、メンバーと一緒に、私はこれを行います:
UILabel *lblMessage;
...
@property (nonatomic, retain)UILabel *lblMessage;
...
- (void)viewDidLoad {
UILabel *localMessage = [[UILabel alloc] initWithFrame:CGRectMake(81, 384, 148, 21)];
localMessage.text = @"I'm a Label!";
localMessage.AutoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleBottomMargin);
self.lblMessage = localMessage;
[localMessage release];
[self.view addSubview:lblMessage];
[super viewDidLoad];
}
しかし、私もそれが行われているのを見ました
...
- (void)viewDidLoad {
UILabel *localMessage = [[UILabel alloc] initWithFrame:CGRectMake(81, 384, 148, 21)];
localMessage.text = @"I'm a Label!";
localMessage.AutoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleBottomMargin);
self.lblMessage = localMessage;
[self.view addSubview:localMessage];
[localMessage release];
[super viewDidLoad];
}
私の最初のiPhone3開発では、そのように:SDKブックを探索します。ローカル変数を追加してから解放することに注意してください。どちらをすればいいですか?それはまったく重要ですか?