2017-11-23 7 views
0

私はIOSアプリケーションを開発しています。私は、動的テキストフィールドやボタンを作成しています、と私は、ボタンをクリックしたときに、テキストフィールドのテキストを取得したいが、私はエラーを取得しています:ボタンをクリックしてTextFieldテキストを取得する

- (IBAction)customFieldAdd:(id)sender{ 
    [_addfieldArray addObject:_field]; 

    x = 10; 
    y = 10; 
    for(int i = 0; i < [_addfieldArray count]; i++) { 
     UITextField *copyfield = [[UITextField alloc]initWithFrame:CGRectMake(5.0, x + _field.frame.size.height, 195.0f, 30.0f)]; 
     [copyfield setDelegate:self]; 
     [copyfield setTag:i]; 
     [_filterPossibleValueView addSubview:copyfield]; 
     x = x+_field.frame.size.height+10; 

     UIButton *copyAddButton = [[UIButton alloc]initWithFrame:CGRectMake(202.0f, y + _addField.frame.size.height, 30.0f, 30.0f)]; 
     [copyAddButton setTag:i]; 
     [copyAddButton addTarget:self action:@selector(customFieldDelete:) forControlEvents:UIControlEventTouchUpInside]; 
     [_filterPossibleValueView addSubview:copyAddButton]; 
     y = y+_addField.frame.size.height+10; 

     count++; 
    } 
} 

- (IBAction)customFieldDelete:(id)sender{ 
    UIButton *button = (UIButton*)sender; 
    NSInteger index = button.tag; 
// [_addfieldArray removeObjectAtIndex:index]; 

    UITextField *field = [_filterPossibleValueView viewWithTag:index]; 
    NSString *myText = field.text; 
} 
+0

あなたがボタンで指定したタグで間違いが解決された場合は、問題を解決します – iPatel

+0

'UITextField * field = [_filterPossibleValueView viewWithTag:index];'このコード行は、 'UITextField'ではなく' UIView'を返しています。あなたのタグを確認してください。 – iPeter

答えて

0

The default value for the tag property is 0:ここ

[UIView setText:]: unrecognized selector sent to instance

は私のコードですその値をテキストフィールドの1つに設定しています。したがって、tag == 0でビューを取得しようとすると、そのタグで複数のビューが表示されます。

あなたはタグを設定する際のオフセットを使用する必要があります

kOffsetは、このように(例えば)に定義されて
[copyfield setTag:kOffset + i]; 
... 
[copyAddButton setTag:kOffset*2 + i]; // note that the button and the text field should not have the same tag either 

let kOffset: Int = 1000 

次に、テキストフィールドのテキストを取得するには、次のことができ

NSInteger tag = button.tag - kOffset; 
UITextField *field = [_filterPossibleValueView viewWithTag:tag]; 
NSString *myText = field.text; 
+0

UITextField *フィールド= [_filterPossibleValueView viewWithTag:index]; NSString * myText = field.text; NSLog(@ "%@"、myText);ボタンクリックでTextFieldテキストを検出する方法 –

関連する問題