0

我正在为我的应用程序(基本 SDK:iOS5)使用 Facebook SDK 3.0,并使用 Scrumptious 应用程序(示例应用程序)作为教程。如果 Facebook 没有打开会话,则会以模态方式呈现登录视图控制器。但它没有以模态方式呈现。这是代码;

- (void)showLoginView {
    UIViewController *topViewController = [self.navigationController topViewController]; // not nil, RootViewController kind of class
    UIViewController *modalViewController = [topViewController modalViewController]; // nil since not modalviewcontroller exists

    if (![modalViewController isKindOfClass:[FacebookLoginViewController class]]) {
        FacebookLoginViewController *loginViewController = [[FacebookLoginViewController alloc] initWithNibName:@"FacebookLoginViewController" bundle:nil]; // allocated, no problem
        [topViewController presentModalViewController:loginViewController animated:NO]; // not working????

    } else {
        FacebookLoginViewController* loginViewController = (FacebookLoginViewController*)modalViewController;
        [loginViewController loginFailed];
    }
}

细节;

  • showLoginView 被调用application:didFinishLaunchingWithOptions:并在分配了 navigationController 之后被调用
  • RootViewController调用前导航控制器的调用viewDidLoad方法showLoginView
  • in showLoginView,topViewController不是 nil (应该是RootViewController:)
  • navigationController 是一种 UINavigationController 类(不是自定义的)
  • loginViewController( FacebookLoginViewController) 的init方法被调用,但不是它的viewDidLoad方法)

日志输出:

2013-03-21 20:01:28.729 RootViewController: viewDidLoad

2013-03-21 20:01:28.735 FacebookLoginViewController:已分配

4

3 回答 3

5

尝试强制您的 UI 代码在主线程上运行:

dispatch_async(dispatch_get_main_queue(), ^{
    [topViewController presentModalViewController:loginViewController animated:NO];
});
于 2013-03-21T18:51:09.153 回答
1

In your topViewController, declare an instance of FacebookLoginViewController. @property (nonatomic, strong) FacebookLoginViewController *fbVC; This will give that viewController a reference to the controller you want to present. The reason it's allocating but not presenting, is because your topViewController is currently being asked to present an object it doesn't own. You need to pass it ownership.

Add topViewController.fbVC = loginViewController; and change [topViewController presentModalViewController:loginViewController animated:NO]; to [topViewController presentViewController:topViewController.fbVC animated:NO completion:nil];

于 2013-03-21T18:49:10.613 回答
0

假设您保留对应用程序窗口的引用,您应该调用[self.window.rootViewController present...]
另请注意,当另一个 VC 已经以模态方式呈现时它可能不起作用(取决于您的目标 iOS 版本 - 实际上您应该使用presentViewController:animated:completion:而不是使用“模态”措辞的方法)

于 2013-03-21T19:31:31.167 回答