我正在尝试使用OAuth 2.0。这会打开一个窗口,其中包含一个UIWebView作为画布,供身份验证服务器进行通信。现在,它被显示为具有自己的视图控制器的模态视图,并且没有后退按钮或取消按钮。因此,用户无法逃离登录过程。
我希望通过导航控制器处理webView,以便我可以推送webView的视图控制器。
我遇到这样的问题。在我看来,我应该能够创建一个UINavigationController对象,根视图控制器是主视图控制器,就像这样
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self];
但是,此行会在调试器中没有任何错误消息的情况下崩溃程序。
我以为我可以在上面的行之后立即放置一行来推动Web视图控制器,如下所示:
[navController pushViewController:windowController animated:YES];
但是,如果没有超越第一行,我就无法开始详细说明要显示的Web视图,然后配置后退按钮。
更新
以下是一些背景信息。这是在单个视图应用程序中。以下代码位于主视图控制器中,它将显示登录对话框。我想通过将windowController推送到navController堆栈来替换调用presentModalViewController的最后一行。请注意导航控制器初始化所在的注释代码。
- (IBAction)signInClicked:(id)sender {
if (![self isSignedIn]) {
// Sign in
[self runSigninThenInvokeSelector:@selector(updateUI)];
}
[self updateUI];
}
- (void)runSigninThenInvokeSelector:(SEL)signInDoneSel {
NSString *clientID = mClientId;
NSString *clientSecret = mClientSecret;
// Show the OAuth 2 sign-in controller
NSString *scope = [GDataServiceGoogleBlogger authorizationScope];
GTMOAuth2ViewControllerTouch *windowController;
windowController = [[GTMOAuth2ViewControllerTouch controllerWithScope:(NSString *)scope
clientID:(NSString *)clientID
clientSecret:(NSString *)clientSecret
keychainItemName:(NSString *)kKeychainItemName
delegate:(id)self
finishedSelector:@selector(windowController:finishedWithAuth:error:)] retain];
//UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self]; // This causes a crash when it is not commented out.
[self presentModalViewController:windowController animated:YES];
}
应用程序委托didFinishLaunchingWithOptions以这种方式设置:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[blogSpotViewController alloc] initWithNibName:@"myNibName" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
答案 0 :(得分:1)
好吧,你可以把它作为一个模态控制器。你只需要安排调用dismissModalViewController:响应'windowController'上的按钮或其他事件。
但是如果你想使用导航控制器,那么你可以在应用程序中设置它:didFinishLaunchingWithOptions:像这样:
// create window here like now
blogSpotViewController *viewController = [[blogSpotViewController alloc] initWithNibName:@"myNibName" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[viewController release];
// TODO: hold onto this navController in a property if you really need that.
self.window.rootViewController = navController;
[navController release];
// present window here as you are now
这会将您的主视图控制器嵌入导航控制器中。然后回到你的runSigninThenInvokeSelector:你可以......
[self.navigationController pushViewController:windowController animated:YES];
希望指出你正确的方向。