【问题标题】:How to incorporate textfield change notification into textFieldDidEndEditing如何将文本字段更改通知合并到 textFieldDidEndEditing
【发布时间】:2014-11-18 02:59:50
【问题描述】:

我有一个带有自定义UITableViewCellsUITableView,每个tableview 单元格都有一个UITextField。默认情况下,文本字段已经有一个用户可以编辑的标题。文本字段中的默认标题与NSFileManager 中的文件相关联,当用户完成文本字段编辑并点击“返回”时,将调用将文件名更改为用户输入内容的方法。这工作正常,但是当用户点击文本字段但不进行任何编辑然后点击“返回”以转到上一个视图控制器时,我收到来自 NSFileManager 的警告,说文件名已经存在。这不会导致任何问题,但它很烦人。我知道除非用户编辑文本字段,否则不应调用调用 NSFileManager 来更改文件名的方法,但我不确定实现这一点的最佳方法。

我看到了这篇文章,但不知道如何将它融入我正在做的事情中: UITextField text change event

我想知道是否有人可以给我一些关于如何完成这项工作的提示。

-(void) textFieldDidEndEditing:(UITextField *)textField
{
    textField.delegate = self;

    NSArray* cells = [self.audioTable visibleCells];
    for (OSAudioTableCell* cell in cells)
    {
        if (textField == cell.textField)
        {
            NSInteger index = cell.tag;  
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
            Recording * recording = [self.fetchCon objectAtIndexPath:indexPath];

            NSString * previousPath = recording.audioURL;

            //I left a lot out, but this is where I call the method to change the file name

           NSString * returnedURL = [self.managedDocument changeFileName:previousPath withNewComponent:textField.text error:&aError];
         }
     }
}

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    我会检查 textField 的文本是否更改。如果确实如此,则通过您在上面粘贴的块。如果没有,那就什么都不做。您可以通过在进行任何编辑之前保留对文本字段值的临时引用来做到这一点:

    // At the top of your class
    @property (strong, nonatomic) NSString *currentFileName; 
    
    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
         _currentFileName = textField.text; 
    }
    

    然后在你上面的方法中,我会检查两个字符串是否不相等:

    -(void) textFieldDidEndEditing:(UITextField *)textField
    {
         if (![textField.text isEqualToString:currentFileName]) {
              // continue with your logic
         }
    }
    

    【讨论】:

      【解决方案2】:

      试试这个。添加委托方法-(void)textFieldDidBeginEditing:(UITextField *)textField。在这种方法中执行以下操作:

      -(void)textFieldDidBeginEditing:(UITextField *)textField {
          self.textBeforeEditing = textField.text;
      }
      

      然后,在调用textFieldDidEndEditing 时进行比较:

      -(void) textFieldDidEndEditing:(UITextField *)textField {
          ...
          if(![self.textBeforeEditing isEqualToString:textField.text]) {
              // Change the file name
          }
          ...
      }
      

      【讨论】:

        【解决方案3】:

        您可以实现textFieldDidBeginEditing:,将 UITextField 的未编辑值存储在实例变量中。然后,在textFieldDidEndEditing: 中简单地比较之前和之后的值,如果它们不同,请像往常一样调用NSFileManager 方法。

        示例

        @interface MyClass () {
            @property (strong, nonatomic) NSString *originalText;
        }
        
        @implementation MyClass
            - (void)textFieldDidBeginEditing:(UITextField *)textField {
                self.originalText = textField.text;
            }
        
            - (void)textFieldDidEndEditing:(UITextField *)textField {
                if ([self.originalText isEqualToString:textField.text]) {
                    // Your original code here.
                }
        
                self.originalText = nil;
            }
        @end
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-07-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多