Iphone UIAlertView(如果登录名或密码为空)

Iphone UIAlertView(如果登录名或密码为空),iphone,ios,uialertview,Iphone,Ios,Uialertview,我正在创建一个简单的登录页面,如果密码或登录字段为空,则需要在其中显示AlertView。有人能帮我吗 -(void)login{ if(username.text.length == 0 || password.text.length == 0){ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Complete All Fields" message:@"You must complete all field

我正在创建一个简单的登录页面,如果密码或登录字段为空,则需要在其中显示AlertView。有人能帮我吗

-(void)login{
    if(username.text.length == 0 || password.text.length == 0){
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Complete All Fields" message:@"You must complete all fields to login!" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
    [alert show];
    [alert release]
    }
}
这应该可以做到
-(void)login
是附加到某些触摸事件的方法,例如当用户点击“登录”按钮或其他什么时


username
password
UITextField
s

这个问题归结为两个主要的子问题

如何检查字符串是否为空

NSString
提供了一个名为
length
的方法,该方法将返回该字符串实例中的字符数


:

返回接收器中的Unicode字符数

- (NSUInteger)length
返回值

接收器中的Unicode字符数

- (NSUInteger)length
讨论

返回的数字包括组合字符序列的各个字符,因此不能使用此方法确定打印时字符串是否可见或显示多长时间


假设存在一个名为name的变量,类型为
NSString

if ([name length] == 0)
{
    NSLog(@"the given name is empty!");
}

如何显示警报

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login Error"
                                                message:@"The given name is empty."
                                               delegate:nil
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
[alert show];

有关更多信息,请参阅。

在这里提问之前,请先做一些研发工作。此语句中的运算符可能应该是合乎逻辑的,而不是or。这样做有什么不对?