将presentModalViewController 与故事板结合使用

发布于 2024-12-28 17:23:43 字数 770 浏览 4 评论 0原文

我对 iOS 编程相当陌生,我正在开发一个 iPad 应用程序,该应用程序有一个选项卡栏控制器,附有 4 个视图控制器(名为 FirstViewController、SecondViewController 等)。目前,选项卡栏控制器设置为应用程序的默认起点。我希望能够在用户到达该点之前对其进行身份验证,因此我添加了另一个名为 LoginViewController 的视图控制器,该控制器在 Storyboard 中自行浮动。

我想要做的是允许应用程序加载,并在 didFinishLaunching 中显示登录页面,直到身份验证完成,然后关闭它。这几天我一直在四处寻找,但我一直在尝试的一切都失败了。

我最近的尝试是

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];

UINavigationController *loginVC = [storyboard instantiateViewControllerWithIdentifier:@"loginVC"];

loginVC.modalPresentationStyle = UIModalPresentationFullScreen;

[self.window.rootViewController presentModalViewController:loginVC animated:YES];

任何帮助将不胜感激。它编译并运行,但视图根本没有显示,我真的很困惑为什么会发生这种情况。

I'm fairly new to iOS programming and I'm working on an iPad app that has a Tab Bar Controller with 4 View Controllers (named FirstViewController, SecondViewController, etc) attached to it. Currently the Tab Bar Controller is set to be the default starting point of the app. I want to be able to authenticate the user before they reach that point, so I've added another View Controller called LoginViewController that is floating by itself in the Storyboard.

What I want to do is allow the app to load and in didFinishLaunching, display the login page until authentication is complete, then dismiss it. I've been searching around for the past couple days, but everything I've been trying has failed.

My most current attempt was

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];

UINavigationController *loginVC = [storyboard instantiateViewControllerWithIdentifier:@"loginVC"];

loginVC.modalPresentationStyle = UIModalPresentationFullScreen;

[self.window.rootViewController presentModalViewController:loginVC animated:YES];

Any help would be appreciated. It compiles and runs, but the view is not showing up at all and I really confused as to why this is happening.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

西瓜 2025-01-04 17:23:43

问题是我试图将它实例化为 UINavigationController,而实际上它只是一个 UIViewController。在 appDelegate.m 的 applicationDidBecomeActive 中调用它就可以了。

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *loginVC = [storyboard instantiateViewControllerWithIdentifier:@"loginVC"];
loginVC.modalPresentationStyle = UIModalPresentationFullScreen;    
[self.window.rootViewController presentModalViewController:loginVC animated:YES];

The problem was I was trying to instantiate it as a UINavigationController, when in fact it was just a UIViewController. Calling this in applicationDidBecomeActive in appDelegate.m did the trick.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *loginVC = [storyboard instantiateViewControllerWithIdentifier:@"loginVC"];
loginVC.modalPresentationStyle = UIModalPresentationFullScreen;    
[self.window.rootViewController presentModalViewController:loginVC animated:YES];
挽清梦 2025-01-04 17:23:43

您需要从当前显示的 viewController 调用“presentModalViewController”,而不是要显示的视图。可能是这样的:

[self.window.rootViewController presentModalViewController:loginVC animated:YES];

You need to call "presentModalViewController" from the currently displayed viewController, not the view that is to be displayed. Likely, something like this:

[self.window.rootViewController presentModalViewController:loginVC animated:YES];
静赏你的温柔 2025-01-04 17:23:43

在 Swift 2 中,现在是:

if let loginController: LoginViewController = mainStoryboard.instantiateViewControllerWithIdentifier("StoryboardControllerID")  as? LoginViewController {
    loginController.modalPresentationStyle = .FullScreen
    self.window?.rootViewController?.presentViewController(loginController, animated: true, completion: { () -> Void in
        // do stuff!
    })
}

In Swift 2, this is now:

if let loginController: LoginViewController = mainStoryboard.instantiateViewControllerWithIdentifier("StoryboardControllerID")  as? LoginViewController {
    loginController.modalPresentationStyle = .FullScreen
    self.window?.rootViewController?.presentViewController(loginController, animated: true, completion: { () -> Void in
        // do stuff!
    })
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文