2

我正在使用情节提要功能创建一个 iOS5 应用程序。基本结构是:

LoginScreen ---(segue)--> MyScreen ---(退出时按下)------(返回登录屏幕)-->LoginScreen

这很简单。我管理第一个 segue 的方式是:

- (void) onResponse:(NSMutableDictionary *)response {
  NSLog(@"Login successful,token received");
  // if the Login was successful,store the token 
  NSUserDefaults* userPref = [NSUserDefaults standardUserDefaults];    
  [userPref setObject:[response objectForKey:@"Token"] forKey:@"AuthToken"];
  [userPref synchronize];
  //..and let the user getting in
  [self performSegueWithIdentifier:@"showHomeScreen" sender:nil];
}

现在,奇怪的是第一次正确执行了 segue,但是,当我在注销后返回登录屏幕时performSegueWithIdentifier:不再起作用(没有错误消息,根本没有任何反应)。不知道发生了什么。哪个可能是问题?

我附上了故事板的屏幕截图..您可以在右上角看到循环: 在此处输入图像描述

多谢!

克劳斯

4

1 回答 1

4

看起来 LoginVC 连接到多个 Segue。

处理登录过程的最佳方法是使用登录 ViewController 的委托。然后在主 VC 中,检查凭据或其他内容,如果需要,请为 LoginVC 调用 performSegue。登录成功后,调用委托方法,Main VC 将关闭模式视图。LoginVC 真的不应该是导航的一部分,也不应该连接到除 Main VC 之外的任何其他 Segue。如果您需要,我有一个完整的示例,但是使用委托方法很容易实现。

给你:LoginViewController.h:

@protocol LoginViewControllerDelegate
    -(void)finishedLoadingUserInfo;
@end

@interface LoginViewController : UIViewController <UITextFieldDelegate>{
    id <LoginViewControllerDelegate> delegate;
}

登录视图控制器.m:

@synthesize delegate;

- (void) onResponse:(NSMutableDictionary *)response {
  NSLog(@"Login successful,token received");
  // if the Login was successful,store the token 
  NSUserDefaults* userPref = [NSUserDefaults standardUserDefaults];    
  [userPref setObject:[response objectForKey:@"Token"] forKey:@"AuthToken"];
  [userPref synchronize];
  //..and let the user getting in
  [delegate finishedLoadingUserInfo];
}

在仪表板 VC .m 文件中:

#pragma mark - LoginViewController Delegate Method
-(void)finishedLoadingUserInfo
{    
    // Dismiss the LoginViewController that we instantiated earlier
    [self dismissModalViewControllerAnimated:YES];
    
    // Do other stuff as needed
}

所以要点是在应用程序加载时检查凭据,如果需要,调用(在仪表板 VC 中):

[self performSegueWithIdentifier:@"sLogin" sender:nil];

然后在 prepareForSegue 方法中(在 Dashboard VC 中):

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"sLogin"]) {
        LoginViewController *livc = segue.destinationViewController;
        livc.delegate = self; // For the delegate method
    }
}

确保命名 Segue sLogin 否则这将不起作用:)

故事板

于 2012-02-14T13:13:27.743 回答