問題描述
我正在使用情節(jié)提要功能創(chuàng)建一個 iOS5 應(yīng)用程序.基本結(jié)構(gòu)是:
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
這很簡單.我管理第一個轉(zhuǎn)場的方式是:
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];
}
現(xiàn)在,奇怪的是第一次正確執(zhí)行了 segue,但是,當(dāng)我在注銷后返回登錄屏幕時,performSegueWithIdentifier: 不再起作用(沒有錯誤消息,根本沒有任何反應(yīng)).不知道發(fā)生了什么.可能是哪個問題?
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?
我附上了故事板的屏幕截圖..您可以在右上角看到循環(huán):
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 中,檢查憑據(jù)或其他內(nèi)容,如果需要,請為 LoginVC 調(diào)用 performSegue.登錄成功后,調(diào)用委托方法,Main VC 將關(guān)閉模式視圖.LoginVC 真的不應(yīng)該是導(dǎo)航的一部分,也不應(yīng)該連接到除 Main VC 之外的任何其他 Segue.如果您需要,我有一個完整的示例,但是使用委托方法很容易實現(xiàn).
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
}
所以要點是在應(yīng)用加載時檢查憑據(jù),如果需要,調(diào)用(在 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 :)
這篇關(guān)于iOS 5 Segue 在第一次執(zhí)行后不工作的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!