2012-10-12 5 views
1

안녕하세요 여러분, UITextView가 비어있을 때 이벤트를 시작하려고합니다. textview가 비어있는 경우 작업을 트리거하는 방법에 문제가 있습니다. UITextView가 비어 다음 FUNC 내가 자동으로 텍스트 뷰가 현재 비어 감지하기 위해 문제가 있습니다UITextfield가 비어있는 경우 자동 실행

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

을 수행로 pe 것입니다 될 때마다 내 말은. 미리 감사드립니다.

답변

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]; 
+0

안녕하세요, 감사합니다. 저는 텍스트보기 용으로 사물을 사용했는데 다시 감사드립니다. :) –

+0

하지만 Repo가 충분하지 않기 때문에 충분한 레포를 얻을 수있을 것입니다. –

+0

오, 저기 있어요 :) –

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:을 사용할 수 있도록 필드,이 메소드가 반환 때까지 비워 둘 수 없습니다 점에 유의하는 것이 유용 .

또한 UITextFieldTextDidChangeNotification 이름에 대한 알림 관찰자를 추가 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;

+0

형제는 언급. 나는 실수를했다. 텍스트 뷰로 도와주세요. 나는 또한 나의 질문을 바로 잡을 것이다. 감사. –

1

이를 추가 할 수 있습니다

[[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]; 
} 
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; 
    } 
} 
이 질문에 질문을 받았다 이후
1

상황이 변경되었을 수,하지만 비슷한 작업 이후 나는 내 대답에 기여하고 싶었다.

Xcode 5 이후로는 메소드를 만들고이를 Editing Changed 보낸 이벤트와 연관시켜야합니다. 내 수업에서

가, 내가 IBAction을 반환하는 방법 작성 : 다음

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

을, 내가 할 필요가 모든 마우스 오른쪽 버튼을 클릭 클래스로 끌어 방법을 선택했다.난의 UITextField를 UITextView를하지 사용하고 있음을 죄송합니다

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

관련 문제