iOS 5 Segue 在第一次执行后不工作 [英] iOS 5 Segue not working after the first execution

查看:19
本文介绍了iOS 5 Segue 在第一次执行后不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating an iOS5 application using the storyboard features. The basic structure is:

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

LoginScreen ---(segue)--> MyScreen ---(press on logout)------(segue back to login screen)-->LoginScreen

这很简单.我管理第一个转场的方式是:

it's pretty simple. The way I manage the first segue is:

- (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: 不再起作用(没有错误消息,根本没有任何反应).不知道发生了什么.可能是哪个问题?

Now,the strange thing is that the segue is correctly performed the first time,but,when I come back to the login screen after a logout the performSegueWithIdentifier: doesn't work anymore (no error messages,simply nothing happens). Not sure what's going on. Which might be the problem?

我附上了故事板的屏幕截图..您可以在右上角看到循环:

I attach a screenshot of the storyboard..you can see the loop in the top-right corner:

非常感谢!

克劳斯

推荐答案

LoginVC好像连接了不止一个Segue.

It looks like that LoginVC is connected to more than one Segue.

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

The best way to handle that Login process is to use a delegate for the Login ViewController. Then in the main VC, you check credentials or whatever and if needed call the performSegue for the LoginVC. When the Login is successful, you call the delegate method and the Main VC will dismiss the modal view. The LoginVC really shouldn't be part of the navigation or connected to any other Segues other than the one from the Main VC. I have a complete example if you need it, but this is easy to implement using delegate methods.

给你:LoginViewController.h:

Here ya go: LoginViewController.h:

@protocol LoginViewControllerDelegate
    -(void)finishedLoadingUserInfo;
@end

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

LoginViewController.m:

LoginViewController.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];
}

在 Dashboard VC .m 文件中:

In the Dashboard VC .m file:

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

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

So the gist is to check for credentials when the app loads and if needed, call (in the Dashboard VC):

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

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

Then in the prepareForSegue method (in the 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 否则这将不起作用:)

Make sure to name the Segue sLogin or this won't work :)

这篇关于iOS 5 Segue 在第一次执行后不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆