2012-03-13 72 views
0

我正在創建一個iPad應用程序,在此我動態地在設備中創建接口,這裏是代碼。如何分配名稱以在iPad應用程序中動態創建對象

- (void)setUpInterface{ 
    UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 10,self.view.frame.size.width-20, self.view.frame.size.height-20)]; 

    int y = 0; 
    for (int i=0; i < [labelArray count]; i++) { 
     y=y+75; 
     UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, y, 500, 30)]; 
     label.text = [NSString stringWithFormat:@"%@",[labelArray objectAtIndex:i]]; 
     label.backgroundColor = [UIColor clearColor]; 
     label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth; 
     [scrollview addSubview:label]; 

     UITextField *textBox = [[UITextField alloc]initWithFrame:CGRectMake(200, y, 500, 30)]; 
     textBox.borderStyle = UITextBorderStyleRoundedRect; 
     textBox.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth; 
     [scrollview addSubview:textBox]; 
    } 

    scrollview.contentSize = CGSizeMake(self.view.frame.size.width-100, y*2); 
    scrollview.backgroundColor = [UIColor scrollViewTexturedBackgroundColor]; 
    scrollview.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth; 

    [self.view addSubview:scrollview]; 

} 

在當我點擊一個按鈕,我想最終獲得在每個文本框中分別輸入的數據。我怎樣才能實現呢?如何爲每個對象分配名稱? 謝謝。

+0

當您按下按鈕時,您是否想要在所有texfields中獲取文本?或者當您按下按鈕時,您想要在特定文本字段中獲取文本? – janusbalatbat 2012-03-13 07:21:08

+0

我想從所有文本字段中獲取文本,當我按下按鈕 – Mithuzz 2012-03-13 07:31:32

+0

時使用快速枚舉。張貼我的答案 – janusbalatbat 2012-03-13 07:35:57

回答

2
for (UITextField *textField in [self.scrollview subviews]) 
{ 
    if([textField isKindOfClass:[UITextField class]]) 
    { 
     NSLog(@"text == %@", textField.text); 
    } 
} 
+0

多數民衆贊成在一個不錯的主意,但我只得到在你的情況[scrollview子視圖]的最後一個文本框 – Mithuzz 2012-03-13 07:39:54

+0

的內容,只是認爲你可能錯過了。 – janusbalatbat 2012-03-13 07:42:33

+0

我用於(UITextField * textField在[self.scrollview子視圖]) { NSLog(@「text ==>%@」,textBox.text); }但只有來自上一個文本框的內容 – Mithuzz 2012-03-13 08:09:16

0

您可以使用標籤屬性。即:

textBox.tag = 1; 
textBox.tag = 2; 
etc. 

不幸的是,這些必須是整數,但可以使地圖整數爲名稱,如果你想要的。

+0

我編輯回答了一下。不要使用0的標籤,因爲默認情況下,除非另有說明,否則所有文本字段都將使用0。 – mikewoz 2012-03-13 06:32:57

+0

我可以給文本框分配標籤值,但是如何從文本框中獲得每個標籤值的字符串? – Mithuzz 2012-03-13 07:17:22

0

將文本字段設置爲成員變量以從任何方法訪問它們。

+0

我怕怎麼實現? – Mithuzz 2012-03-13 07:34:14

相關問題