0

我有一些动态分配的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);
    }
}

}

已编辑:我正在存储的文本字段数据被设为 nil,但在文本字段上,数据仍在保留,它没有被删除,对于文本字段我添加了背景图像,不知道它是否有任何区别,我附上它的代码:

    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];
4

1 回答 1

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 = @"";
       }
   }

这将删除text视图中每个 UITextField 中的 。如果您只需要清除某些文本字段,请取消注释代码;这将检查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

于 2012-05-01T09:26:29.850 回答