UITextfield变空时自动操作

时间:2012-10-12 06:26:49

标签: iphone ios uitextfield ios6

嘿伙计们,我正试图在UITextView变空时启动一个事件。我遇到的问题是当textview变空时如何触发操作。我的意思是每当UITextView在func执行后变为空白

 -(void)textViewIsEmpty
 {
      NSLog(@"Text View Is Empty");
 }

我遇到问题,以便自动检测textView现在为空。 在此先感谢。

5 个答案:

答案 0 :(得分:2)

自从提出这个问题以来,事情可能已经发生了变化,但是我想做出自己的答案,因为我正在研究类似的事情。

从Xcode 5开始,您只需创建一个方法并将其与Editing Changed已发送事件相关联。

在我的班级中,我创建了一个返回IBAction的方法:

- (IBAction)priceFieldEdited:(id)sender {
    [self refreshFetchedResultsAndTable];
}

然后,我需要做的就是右键单击拖动到该类并选择方法。

Example of associating the text field to the method in the class.

答案 1 :(得分:1)

UITextFieldTextDidChangeNotification名称添加通知观察员:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(textFieldDidChanged:)
                                             name:UITextFieldTextDidChangeNotification
                                           object:self.yourTextField];

textFieldDidChanged:是一种自定义方法,用于在文本字段更改时管理操作。您可以判断文本字段的长度。


编辑:

textFieldDidChanged:的示例代码:

- (void)textFieldDidChanged:(NSNotification *)notification {
  if (self.yourTextField.length)
    return;
  [self textFieldIsEmpty];
}

答案 2 :(得分:1)

使用 textFieldDidEndEditing 委托方法。

- (void)textFieldDidEndEditing:(UITextField *)textField
{
   if([textField.text stringByReplacingOccurrencesOfString:@" "
                                                        withString:@""].length >0)
      NSLog(@"Text Field Is Empty");
      //or perform selector textFieldIsEmpty
}

UITextFieldTextDidChangeNotification 名称添加 通知观察员:

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(textFieldChanged:)
                                         name:UITextFieldTextDidChangeNotification
                                       object:self.yourTextField];

选择器将是这样的:

-(void)textFieldChanged:(NSNotification*)notification
{
    UITextField *txt = (UITextField *)[notification object];
     if([txt.text stringByReplacingOccurrencesOfString:@" "
                                                        withString:@""].length >0)
      NSLog(@"Text Field Is Empty");
}

编辑:不需要时删除通知:

[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];

答案 3 :(得分:0)

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSRange textFieldRange = NSMakeRange(0, [textField.text length]);
    if (NSEqualRanges(range, textFieldRange) && [string length] == 0) {
        // Game on: when you return YES from this, your field will be empty
    }
    return YES;
}

请注意,在此方法返回之前该字段不会为空,因此您可能希望在此处设置一些中间状态,然后使用textFieldDidEndEditing:知道用户已完成清空该字段

您也可以将其添加到- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;

答案 4 :(得分:0)

使用此:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"RETURN ON KEYBORAD - PRESSED");

    if ( ([ textField.text length ] == 0) | ([[textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ] length]  == 0))
    {
        [self textFieldIsEmpty];
        return YES;
    }
}
相关问题