2011-06-16 93 views
2

當我嘗試以編程方式創建11個文本字段時,我很掙扎。問題是文本字段不顯示。我在viewDidLoad方法中創建它們。以編程方式創建UITextField時出現問題iphone

下面是我使用的代碼:

- (void)viewDidLoad 
{ 
    // Determine some basic info 
    int numberOfTextfields = 11; 
    int textfieldHeight = 40; 
    int textfieldWidth = 200; 


    // Create the UIScrollView 
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, numberOfTextfields*textfieldHeight,textfieldWidth)]; 


    // Create all the textfields 
    NSMutableArray *textfields = [NSMutableArray arrayWithCapacity: 
            (NSUInteger)numberOfTextfields]; 
    for(int i = 0; i < numberOfTextfields; i++) { 
     UITextField *field = [[UITextField alloc] initWithFrame: 
           CGRectMake(0,i*textfieldHeight,textfieldHeight,textfieldWidth)]; 

     [scrollView addSubview:field]; 
     [textfields addObject:field]; 
    } 

    [super viewDidLoad]; 

} 

他們爲什麼不顯示任何線索?

在此先感謝。

+0

你在哪裏把scrollview本身? – 2011-06-16 12:07:23

回答

5

你似乎永遠不會添加scrollView作爲子視圖。


您的textFields已被隱藏,因爲您從未設置過邊框樣式。試試這個:

- (void)viewDidLoad 
{ 
    // Determine some basic info 
    int numberOfTextfields = 11; 
    int textfieldHeight = 40; 
    int textfieldWidth = 200; 


    // Create the UIScrollView 
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, textfieldWidth,numberOfTextfields*textfieldHeight)]; 
    scrollView.contentSize = CGSizeMake(numberOfTextfields*textfieldWidth, textfieldHeight); 

    // Create all the textfields 
    NSMutableArray *textfields = [NSMutableArray arrayWithCapacity: 
            (NSUInteger)numberOfTextfields]; 
    for(int i = 0; i < numberOfTextfields; i++) { 
     UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(0,i*textfieldHeight,textfieldWidth,textfieldHeight)]; 
     field.borderStyle = UITextBorderStyleRoundedRect; 
     [scrollView addSubview:field]; 
     [textfields addObject:field]; 
    } 

    [self.view addSubview:scrollView]; 

    [super viewDidLoad]; 
} 
+0

我試過[self.view addSubview:scrollView];但它不起作用,這就是爲什麼我從代碼中刪除它。 – madcoderz 2011-06-16 12:03:15

+0

邊框風格...好的謝謝。一個問題:我怎樣才能將文本字段與海誓山盟分開? – madcoderz 2011-06-16 12:30:10

+0

只需添加一些填充。當你創建'UITextField'時,執行以下操作:'UITextField * field = [[UITextField alloc] initWithFrame:CGRectMake(0,i * textfieldHeight + 10,textfieldWidth,textfieldHeight)];'。一定要適當增加滾動視圖的'contentSize'。 – 2011-06-16 13:01:14

0

您需要添加UIScrollView到您的視圖設置其內容區域,因爲它不會自動地管理它(你可能已經預期)。

0

您需要將您的滾動視圖添加到您的視圖控制器。如果它被分配,
別的爲此 self.view = [[UIView alloc] initWithFrame:CGRectMake (0,0,320,460)] 在端[self.view addSubview: scrollView]

0

檢查這代碼.....幀參數是(X,Y,寬度,高度)

int numberOfTextfields = 11; 
    int textfieldHeight = 40; 
    int textfieldWidth = 200; 


    // Create the UIScrollView 
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,textfieldWidth,numberOfTextfields*textfieldHeight)]; 


    // Create all the textfields 
    NSMutableArray *textfields = [NSMutableArray arrayWithCapacity: 
            (NSUInteger)numberOfTextfields]; 
    for(int i = 0; i < numberOfTextfields; i++) { 
     UITextField *field = [[UITextField alloc] initWithFrame: 
           CGRectMake(0,i*textfieldHeight,textfieldWidth,textfieldHeight)]; 
     field.borderStyle=UITextBorderStyleRoundedRect; 
     [scrollView addSubview:field]; 
     [textfields addObject:field]; 
    } 
    [self.view addSubview:scrollView]; 
    [super viewDidLoad]; 
0
添加此行

嘗試下面的代碼。文本框被添加,您可以滾動文本框。

 
int numberOfTextfields = 11; 
int textfieldHeight = 40; 
int textfieldWidth = 200;

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,textfieldWidth,numberOfTextfields*textfieldHeight)]; scrollView.contentSize = CGSizeMake(textfieldWidth, numberOfTextfields*textfieldWidth+10);

NSMutableArray *textfields = [NSMutableArray arrayWithCapacity: (NSUInteger)numberOfTextfields];

for(int i = 1; i < numberOfTextfields; i++) { UITextField *field = [[UITextField alloc] initWithFrame: CGRectMake(0,i*textfieldHeight,textfieldWidth,textfieldHeight)]; field.borderStyle=UITextBorderStyleRoundedRect; [scrollView addSubview:field]; [textfields addObject:field]; }

[self.view addSubview:scrollView]; [super viewDidLoad];

相關問題