iOS 5第一次执行后Segue无法正常工作 [英] iOS 5 Segue not working after the first execution

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

问题描述

我正在使用故事板功能创建iOS5应用程序。基本结构是:

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

LoginScreen ---(segue) - > MyScreen ---(按下注销)------(segue返回登录屏幕) - > LoginScreen

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

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

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:

非常感谢!

Claus

推荐答案

看起来LoginVC连接到多个Segue。

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

处理登录过程的最佳方法是使用Login ViewController的委托。然后在主VC中,检查凭据或其他任何内容,如果需要,请调用LoginVC的performSegue。登录成功后,您将调用委托方法,主VC将关闭模态视图。 LoginVC实际上不应该是导航的一部分或连接到除主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];
}

在仪表板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
}

所以要点是在应用程序加载时检查凭据,如果需要,请调用(在仪表板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方法中(在仪表板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天全站免登陆