iOS警示框—判断是否有空格和回车

//输入框是否为空(判断空格和回车)
-(BOOL)isEmpty:(NSString *)string
{
    if (!string) {
        return YES;
    }else
    {
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *str = [string stringByTrimmingCharactersInSet:set];
        if (str.length == 0) {
            return YES;
        }else{
            return NO;
        }
    }
}

//警示框
-(void)alert{
    
    NSString *message = nil;
    if([self isEmpty:self.userTextField.text]){    //调用函数把空格处理为0
        message = @"请输入用户名";
    }else if(self.pwdTextField.text.length == 0){    //密码可以带空格
        message = @"请输入密码";
    }
    if(message)
    {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        //    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"警告" style:UIAlertActionStyleDestructive handler:nil];
        [alertController addAction:action1];
        [alertController addAction:action2];
        //    [alertController addAction:action3];
        [self presentViewController:alertController animated:YES completion:nil];
    }
}

  

posted on 2016-03-17 22:12  我就是一种相逢  阅读(483)  评论(0编辑  收藏  举报

导航