【问题标题】:Removing UITextField删除 UITextField
【发布时间】:2026-01-26 22:30:01
【问题描述】:

当用户点击按钮时,我在从滚动视图中动态删除 UITextFields 时遇到了一些问题。 UITextields 是以编程方式创建的。这是我迄今为止所拥有的,任何帮助将不胜感激。

-(IBAction)resetAll{

int textFieldTag;

for (int i=0; i<[array count]; i++) {
    textFieldTag = i + 100;
    UITextField *myTextField = (UITextField *)[self.view viewWithTag:textFieldTag]; 
    [myTextField removeFromSuperview];
    [myTextField release];
}
}

【问题讨论】:

  • 你到底有什么问题?你没有描述发生了什么。
  • 您要删除特定的 UITextField 还是每个 UITextField? -- 再多一些信息就好了。

标签: iphone objective-c xcode ipad uitextfield


【解决方案1】:
-(IBAction)resetAll
{    
    NSMutableArray *arrayTextFields=[yourScrollView subViews]; //get all subviews from your scrollview

for (int i=0; i<[arrayTextFields count]; i++) 
{
    if([[arrayTextFields objectAtIndex:i] isKindOfClass:[UITextField class]]) //check for UITextField
    {
        UITextField *textField=(UITextField *)[arrayTextFields objectAtIndex:i];
        [textField removeFromSuperView]; //Remove textField
    }
}
}

【讨论】:

    【解决方案2】:

    实现这个....

    -(IBAction)resetAll
     {
         for (UITextField *myTextField in [myScrollView subviews]) 
              [myTextField removeFromSuperview];
     }
    

    【讨论】:

      【解决方案3】:

      试试这个:

      -(IBAction)resetAll
       {
        for (UITextField *tf in myScrollView) {
        [tf removeFromSuperview];
        [tf release];
       }
      
      }
      

      【讨论】: