iphone再次访问该页面时清除UITextfield数据

时间:2012-05-01 09:12:04

标签: ios uitextfield

我有一些动态分配的texfields,我在其中输入文字,

现在我有两个问题 1)点击2-3次后,我的意思是更改视图并返回到相同的视图,而不是从文本字段中删除数据,文本字段中的先前数据存在。 我试过的代码是

    answerTextField.text = @"";
    answerTextField.text = nil;

2)我无法删除我附加来自textfield.text

的数据的文本字段数据
      -(void) keyPressed: (NSNotification*) notification { 
if ([[[notification object]text] isEqualToString:@" "])
{
    UITextField *textField = (UITextField *)[self.view viewWithTag:nextTag];
    textField.text = @"";
    [textField becomeFirstResponder];
    nextTag = textField.tag;
}
else
{
    [[NSNotificationCenter defaultCenter] removeObserver: self name:UITextFieldTextDidChangeNotification object: nil];

    NSLog(@"TextField tag value :%d",tagCount);
    NSLog(@"TextField next tagg tag value :%d",nextTag);

    if (tagCount == nextTag+1) 
    {
        UITextField *textField = (UITextField *)[self.view viewWithTag:nextTag];
        [textField resignFirstResponder];
        NSLog(@"append string :%@",appendString);
    }
    else
    {
        NSLog(@"TextField nexttag value before :%d",nextTag);

        nextTag = nextTag + 1;
        NSLog(@"Letter is%@",[[notification object]text]);

        str= [[NSString alloc]initWithFormat:@"%@",[[notification object]text]];
        NSLog(@"TextField String :%@",str);

        NSLog(@"TextField nexttag value after :%d",nextTag);

        [appendString appendString:[NSString stringWithFormat:@"%@",str]];
        NSLog(@"Content in MutableString: %@",appendString);
    }

    NSLog(@"The tags in keypressed:%d",nextTag);
    UITextField *textField = (UITextField *)[self.view viewWithTag:nextTag];
    [textField becomeFirstResponder];
}
// For inserting Spaces taken from array.    
if(tagCount+300 == nextTag)
{
    for (int m=0  ; m< [intArray count]; m++) 
    {
        [appendString insertString:@" " atIndex:[[intArray objectAtIndex:m]intValue]];
        NSLog(@"FinalString : %@",appendString);
    }
}

}

编辑:我正在存储的文本字段数据为零,但在文本字段上数据仍然没有删除,因为我添加了背景图像的文本字段,不知道是否有任何差异与否,我附上代码:

    UIImageView *myView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"text_line.png"]];
                myView.center = CGPointMake(5, 27);                     
                [letterField insertSubview:myView atIndex:0];// mgViewTitleBackGround is image
                [myView release];

1 个答案:

答案 0 :(得分:2)

要清除文本字段,请在viewWillAppear中执行以下代码:

  NSArray *arraysubViews = [self.view subViews];

  for(UIView *subView in arraysubViews){

      if([subView isKindOfClass:[UITextField class]]){
          // if(subView.tag == MY_TEXT_VIEW_TAG)
           (UITextField *)subView.text = @"";
       }
   }

这将删除视图中每个UITextField中的text。如果您只需要清除一些文本字段,请取消注释代码;这将检查tag

修改

  NSArray *arraysubViews = [self.view subviews];

  for(UIView *subView in arraysubViews){

      if([subView isKindOfClass:[UITextField class]]){
          // if(subView.tag == MY_TEXT_VIEW_TAG)
          UITextField *textField = (UITextField *)subView;
          textField.text = @"";
       }
   }

UIView没有subViews方法。而是subViews