我在故事板中创建了一个现在的模态segues,如下图所示。
我的第一个场景有一个带动作的按钮:
- (IBAction)loginButtonPressed:(id)sender {
[self Login];
}
进入这个方法:
- (void)Login
{
NSString *rawString = [self.idTextField text];
NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
[self.idTextField setText:[rawString stringByTrimmingCharactersInSet:whitespace]];
[userName UserLogin:self.idTextField.text andPassWordExists:self.passwordTextField.text completionHandler:^(id responseObject, NSError *error) {
if (responseObject != nil) {
NSString *userN,*name;
NSArray *object = [responseObject objectAtIndex:0];
userN = [object valueForKey:@"userName"];
name = [object valueForKey:@"name"];
self.idTextField = nil;
self.passwordTextField = nil;
LHAppDelegate *appDelegate = (LHAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.loginSession setString:[userN lowercaseString]];
[appDelegate.nameOfUser setString:name];
}else{
[self CustomAlert:@"Sorry Login Failed, User and/or Passsword Incorrect"];
}
[indicatorView stopAnimating];
[indicatorView removeFromSuperview];
indicatorView = nil;
[loadingView removeFromSuperview];
loadingView = nil;
}];
}
我要做的是编程,所以当用户登录时,它们以模态方式存在于标签栏控制器中。
目前,当我点击错误信用的按钮时,我会看到自定义提醒,但它仍然会将我带到标签栏控制器。
基本上我想做的就是围绕现在的模态区域设置一个条件。这可能吗?
我找到了这段代码:
[self performSegueWithIdentifier: @"MySegue" sender: self];
我把它添加到我的Login方法中:
- (void)Login
{
NSString *rawString = [self.idTextField text];
NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
[self.idTextField setText:[rawString stringByTrimmingCharactersInSet:whitespace]];
[userName UserLogin:self.idTextField.text andPassWordExists:self.passwordTextField.text completionHandler:^(id responseObject, NSError *error) {
if (responseObject != nil) {
NSString *userN,*name;
NSArray *object = [responseObject objectAtIndex:0];
userN = [object valueForKey:@"userName"];
name = [object valueForKey:@"name"];
self.idTextField = nil;
self.passwordTextField = nil;
LHAppDelegate *appDelegate = (LHAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.loginSession setString:[userN lowercaseString]];
[appDelegate.nameOfUser setString:name];
[self performSegueWithIdentifier: @"MySegue" sender: self];
}else{
[self CustomAlert:@"Sorry Login Failed, User and/or Passsword Incorrect"];
}
[indicatorView stopAnimating];
[indicatorView removeFromSuperview];
indicatorView = nil;
[loadingView removeFromSuperview];
loadingView = nil;
}];
}
当我使用错误的信用卡登录时仍然会将我带到标签栏控制器
答案 0 :(得分:1)
在上面的场景中,它将始终执行不受服务器响应影响的segue,因为segue是通过按钮单击执行的。 为避免这种情况,请按以下方式进行:
现在,当您获得成功回复时,请进行以下调用
[self performSegueWithIdentifier:@"segueIdentifier" sender:self]
如果要传递参数,请执行以下功能:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
答案 1 :(得分:0)
一种简单的方法是从NSSeguePerforming协议实施- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
。
如果用户未登录则返回NO,否则返回YES,并执行segue /用户将被带到标签栏控制器。
修改强>
由于您已经实现了在登录方法中授权用户的逻辑,我建议执行@manish_kumar建议的内容。