2012-03-13 8 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

ボタンを押したときにすべてのテキストフィールドにテキストを入力しますか?ボタンを押したときに特定のテキストフィールドでテキストを取得したいのですか? – janusbalatbat

+0

ボタンを押すとすべてのテキストフィールドからテキストを取得したい – Mithuzz

+0

高速列挙を使用します。私の答えは – janusbalatbat

答えて

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

thatsすてきなアイデアですが、私は最後のテキストボックス – Mithuzz

+0

のコンテンツをスクロールビューのサブビューで取得しました。 – janusbalatbat

+0

私は([self.scrollview subviews]のUITextField * textField)に使用します。 { NSLog(@ "text ==>%@"、textBox.text); }最後のテキストボックスのコンテンツのみ – Mithuzz

0

tagプロパティを使用できます。つまり、

残念ながら、これらは整数である必要がありますが、必要に応じて整数のマップを作成できます。

+0

です。私は少し答えを編集しました。デフォルトでは、特に指定しない限り、すべてのテキストフィールドには0が使用されるため、0のタグは使用しないでください。 – mikewoz

+0

私はテキストボックスにタグ値を割り当てることができますが、どのように各タグ値でテキストボックスから文字列を取得できますか? – Mithuzz

0

テキストフィールドを任意のメソッドからアクセスするためのメンバ変数として作成します。

+0

私はそれを実装する方法を恐れていますか? – Mithuzz

関連する問題