Ale*_*ber 18 iphone objective-c uinavigationcontroller ios
在Xcode 5.0.2中,我创建了一个iPhone应用程序(完整的应用程序代码在GitHub上,请运行pod install一次 - for SDWebImage),它显示社交网络列表(Facebook,Google +等),然后 - 在另一个视图中 -一个UIWebView显示的OAuth2网页,然后用头像和细节(姓,名等)第三种观点:


在我在上一个视图中显示用户头像和详细信息之前,我将用户数据保存到NSUserDefaults.
我的问题是:当用户下次启动应用程序并且NSUserDefaults已经存在用户数据时- 如何跳过前2个视图并显示(没有任何动画)具有这些用户详细信息的最后一个视图?
我将以下代码放到AppDelegate.m中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *key = [defaults objectForKey:@"key"];
User *user = [User loadForKey:key];
if (user) {
UINavigationController *nc = (UINavigationController*)self.window.rootViewController;
// XXX how to load the last view here? XXX
}
return YES;
}
Run Code Online (Sandbox Code Playgroud)
并且可以看到,用户数据正在加载好,但我不明白如何跳转到最后一个视图(特别是因为大多数UI都是在故事板中定义的)?
更新:我尝试使用AppDelegate.m中的以下代码(并将DetailsStoryboardID 分配给最后一个视图)来关注Tommy Alexander的建议(谢谢!):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *key = [defaults objectForKey:@"key"];
User *user = [User loadForKey:key];
if (user) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *uvc = [storyboard instantiateViewControllerWithIdentifier:@"Details"];
NSLog(@"XXX uvc=%@ user=%@", uvc, user);
[self.window.rootViewController presentViewController:uvc animated:YES completion:nil];
}
return YES;
}
Run Code Online (Sandbox Code Playgroud)
虽然最后一个视图(具有用户头像和详细信息的UserViewController)似乎可以实例化,但是我得到以下警告并且应用程序没有跳转到最后一个视图:
MyAuth[3917:70b] XXX uvc=<UserViewController: 0x8acbac0> user=
VK
59751265
Alexander
Farber
1945522
http://cs319319.vk.me/v319319265/b7df/TnyKeffL_mU.jpg
0
MyAuth[3917:70b] Warning: Attempt to present <UserViewController: 0x8acbac0> on <UINavigationController: 0x8bb3a30> whose view is not in the window hierarchy!
Run Code Online (Sandbox Code Playgroud)
更新2:当我将上面的代码移动到viewDidLoad第一个视图(MasterViewController.m,用户可以选择社交网络)时,我得到另一个警告:
MyAuth[1358:70b] XXX uvc=<UserViewController: 0x8a97430> user=
FB
597287941
Alexander
Farber
Bochum, Germany
http://graph.facebook.com/597287941/picture?type=large
0
MyAuth[1358:70b] Unbalanced calls to begin/end appearance transitions for <UINavigationController: 0x8a893e0>.
Run Code Online (Sandbox Code Playgroud)
更新3:感谢您的宝贵答案!我最终使用了Leonty Deriglazov的建议:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc1 = [storyboard instantiateViewControllerWithIdentifier:@"Master"];
//UIViewController *vc2 = [storyboard instantiateViewControllerWithIdentifier:@"Login"];
UIViewController *vc3 = [storyboard instantiateViewControllerWithIdentifier:@"User"];
User *user = [User load];
NSArray *controllers = (user ? @[vc1, vc3] : @[vc1]);
UINavigationController *nc = (UINavigationController *)self.window.rootViewController;
[nc setViewControllers:controllers];
return YES;
}
Run Code Online (Sandbox Code Playgroud)
Leo*_*zov 14
简而言之,最直接的解决方案是使用-[UINavigationController setViewControllers:animated:].
您需要执行以下操作才能实现您的目标:
-[UINavigationController setViewControllers:]与阵列作为第一个参数.而已.您-[AppDelegate application:didFinishLaunchingWithOptions:]可能看起来像这样(在检查您的跳过条件后):
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc1 = [storyboard instantiateViewControllerWithIdentifier:@"MyAuth"]; //if you assigned this ID is storyboard
UIViewController *vc2 = [storyboard instantiateViewControllerWithIdentifier:@"Login"]; //if you assigned this ID is storyboard
UIViewController *vc3 = [storyboard instantiateViewControllerWithIdentifier:@"Details"];
NSArray *controllers = @[vc1, vc2, vc3];
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
[navController setViewControllers:controllers];
Run Code Online (Sandbox Code Playgroud)
如果你只是粘贴它-[AppDelegate application:didFinishLaunchingWithOptions:],你会发现它立即起作用.
| 归档时间: |
|
| 查看次数: |
8006 次 |
| 最近记录: |