我一直在开发iOS应用,此应用包含一个登录屏幕。此屏幕有两个UITextField,我输入用户名和登录所需的密码。
但是我遇到了一个问题:我已经建立了一个"否则如果"如果字段为空,则触发UIAlertView。
UIAlertView我设置了DOES弹出但......它显示了下一个视图控制器。
另一个问题是...检查用户名和密码是否正确的检查功能被跳过,它也会跳转到下一个视图控制器。
这很奇怪,因为我设置了一个" if"条件检查两个UITextField中的文本是否必须匹配才能触发下一个视图控制器。
我已经预感到与登录操作相关联的另一个方法可能会干扰此过程。
我将发布有关登录的代码段:
- (void)btn_submit:(id)sender{
NSString *user = self.usuari.text;
NSString *pass = self.contrasenya.text;
NSString * urlBase = @"http://www.v2msoft.com/clientes/lasalle/curs-ios/login.php?username=lluis&password=seguro";
[JSONHTTPClient getJSONFromURLWithString:urlBase completion:^(id json,JSONModelError *err){
JsonUser * user = [[JsonUser alloc]initWithDictionary:json error:nil];
if(user.succeed) {
self.user_id = user.user_id;
[self performSegueWithIdentifier:@"Login" sender:self];
}
else{
NSLog(@"error");
}
}];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (([self.usuari.text isEqualToString:@"lluis"]) && ([self.contrasenya.text isEqualToString:@"seguro"]))
{
// [self performSegueWithIdentifier:@"Login" sender:self];
Supermarket * supermercat = segue.destinationViewController;
supermercat.user_id = self.user_id;
}
else if (_usuari.text.length == 0 || _contrasenya.text.length == 0)
{
[[[UIAlertView alloc] initWithTitle:@"Alerta!"
message:@"Camps buits o dades incorrectes!"
delegate: nil
cancelButtonTitle:@"Cancelar"
otherButtonTitles:nil
] show];
}
}
和JsonUser类:
#import "JSONModel.h"
@interface JsonUser : JSONModel
@property BOOL succeed;
@property int user_id;
@end
我知道发送数据不安全并不是一个好主意,但由于它是一个不会被商业化的应用程序,因此它并不重要。
你能帮我解决一下如何正确实现登录功能吗?
答案 0 :(得分:0)
一旦调用prepareForSegue,将始终进行转换。在您的情况下,即使用户名和密码不正确。如果服务器正在检查用户名和密码,那么这行不应该是:
if (([self.usuari.text isEqualToString:@"lluis"]) && ([self.contrasenya.text isEqualToString:@"seguro"]))
也许这就是你要找的东西,这里用户名和密码都是由服务器验证的。如果"成功"如果是,请执行segue,否则会显示警报。
- (void)btn_submit:(id)sender{
NSString *user = self.usuari.text;
NSString *pass = self.contrasenya.text;
NSString * urlBase = [NSString stringWithFormat:@"http://www.v2msoft.com/clientes/lasalle/curs-ios/login.php?username=%@&password=%@", user, pass];
[JSONHTTPClient getJSONFromURLWithString:urlBase completion:^(id json,JSONModelError *err){
JsonUser * user = [[JsonUser alloc]initWithDictionary:json error:nil];
if(user.succeed) {
self.user_id = user.user_id;
[self performSegueWithIdentifier:@"Login" sender:self];
}
else{
[[[UIAlertView alloc] initWithTitle:@"Alerta!"
message:@"Camps buits o dades incorrectes!"
delegate: nil
cancelButtonTitle:@"Cancelar"
otherButtonTitles:nil
] show];
}
}];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
Supermarket * supermercat = segue.destinationViewController;
supermercat.user_id = self.user_id;
}