2011-10-27 13 views
0

viewDidLoadにテキストフィールドを追加しましたが、画面に表示されませんでした。画面にテキストフィールドを表示できません

は、ここで私は

UITextField *newTextField = [[UITextField alloc] init]; 
self.tfText = newTextField; 
//.... 
//all your code here 
//.... 
[newTextField release]; 

を意味するとリリースすることを忘れていけない...

- (void)viewDidLoad{ 
    [super viewDidLoad];  

    [self.view setBackgroundColor:[UIColor lightGrayColor]]; 

    tfText.frame = CGRectMake(65, 100, 200, 50); 
    tfText.backgroundColor = [UIColor whiteColor];  
    [tfText setTextColor:[UIColor blackColor]]; 
    tfText.placeholder = @"Test"; 
    [tfText setBorderStyle:UITextBorderStyleNone]; 
    [self.view addSubview:tfText]; 
} 

答えて

3

はあなたがオブジェクトを初期化する必要があるようです。ここの.h

#import <UIKit/UIKit.h> 
@interface SecondViewController : UIViewController{ 
    UITextField *tfText; 
} 
@property (nonatomic, retain) UITextField *tfText; 
@end 

です.Mだあなたdeallocメソッドのインスタンス

0

D33pN16h7が正しい場合は、オブジェクトをインスタンス化する必要があります。しかし、私は上記とは少し違ったやり方でやっていますが、UITextFieldインスタンスを作成してそれにインスタンスを設定する必要はありません。

self.tfText = [[UITextField alloc] init];

+0

1のカウント、およびプロパティセッター(self.tfText」)を保持するメモリリーク、「ALLOC INIT」増分を生成するので、これは問題であり、また、保持カウントをインクリメントし、私が示すコードあなたはオブジェクトをインスタンス化する正しい方法です。乾杯! – D33pN16h7

+0

素晴らしい情報、ありがとう! – Osiris

0
UITextField entered2 = [[UITextField alloc] initWithFrame:CGRectMake(120.0, 125.0, 150.0, 25.0)]; 
[entered2 setBackgroundColor:[UIColor whiteColor]]; 
[email protected]"Active";  
[self.view addSubview:entered2]; 
[entered2 release]; 
関連する問題