我正在创建一个简单的登录页面,如果密码或登录字段为空,则需要显示AlertView。有人可以帮我吗?
答案 0 :(得分:0)
-(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
。
答案 1 :(得分:0)
这个问题归结为两个主要的子问题;
如何检查字符串是否为空
NSString
提供了一个名为length
的方法,它将返回该字符串实例中的字符数。
length
返回接收器中的Unicode字符数。
- (NSUInteger)length
返回值
接收器中的Unicode字符数。
讨论
返回的数字包括组合字符序列的各个字符,因此您无法使用此方法来确定字符串在打印时是否可见或显示的时间长度。
假设存在一个名为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];
有关详情,请参阅UIAlertView Reference。