UITextView 未拾取文本输入 iOS

UITextView not picking up text input iOS

我有 UITextView,我想在空的时候显示一个警告

.h
@property (nonatomic, strong) IBOutlet UITextView *textField;

.m
- (IBAction)next:(id)sender {
if ([self.textField.text length] == 0) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error!"
                                                                message:@"text empty"
                                                               delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];
}
}

警报未显示,我可以继续前进而无需输入任何文本。谁能帮我弄清楚为什么?

谢谢

textField 可能有空白字符。尝试使用以下方法删除任何可能的空格:

NSString *text = [self.textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet];
if ([text length] == 0) {
    ....
}

试试这个 -

- (IBAction)next:(id)sender {

    NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSString *trimmed = [self.textField.text stringByTrimmingCharactersInSet:whitespace];
    if ([trimmed length] == 0) {
         // Text was empty or only whitespace.
         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error!"
                                                            message:@"text empty"
                                                           delegate:nil 
                                                  cancelButtonTitle:@"OK" 
                                                  otherButtonTitles:nil];
        [alertView show];
   }
}

我从头开始检查它,它运行良好。

Here is download link for testing the project

我确定,textview 没有连接到 IB。

如果它连接到 IB,那么我会说将示例项目上传到 dropbox 以从我们这边检查。