首次启动时以模态方式显示视图控制器

发布于 2025-01-06 17:45:48 字数 2082 浏览 2 评论 0原文

我希望在第一次打开应用程序时显示有关如何使用我的应用程序的说明的视图。我已经使用 NSUserDefaults 处理了在第一次启动时仅显示此视图的问题,但我无法让视图以我希望的方式显示,而不是作为 rootViewController。这是我的 AppDelegate.m 代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];

    self.window.rootViewController = self.viewController;

    BOOL hasShownStartup = [[NSUserDefaults standardUserDefaults] boolForKey:kAppHasShownStartupScreen];

    if (!hasShownStartup) {
        [self showInstructions];
    }

    [self.window makeKeyAndVisible];
    return YES;
}

- (void)showInstructions
{
    NSLog(@"showing instructions");
    InstructionsViewController *howToView = [[InstructionsViewController alloc] initWithNibName:@"InstructionsViewController"
                                                                                     bundle:nil];
    self.instructionsViewController = howToView;
    [howToView release];
    UINavigationController *howtoNavController = [[UINavigationController alloc]
                                             initWithRootViewController:self.instructionsViewController];
    self.instructionsViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self.viewController presentModalViewController:howtoNavController animated:NO];
    [howtoNavController release];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kAppHasShownStartupScreen];
}

​​我希望我的 rootViewController 保留为 self.viewController。我希望能够单击 instructionsViewController 导航栏上的“完成”以转换回 rootViewController。执行编写的代码永远不会显示指令视图。我可以看到 instructionsViewController 的唯一方法是,将行 [self.viewControllerpresentModalViewController:howtoNavControlleranimated:NO]; 更改为 self.window.rootViewController = self.instructionsViewController;但这显然不是我想要的(除非我可以模态转换回 viewController)。

希望我已经足够清楚地说明了我想要实现的目标。提前致谢。

I'd like to have a view with instructions on how to use my app show up on the first time the app is opened. I have handled the problem of the only showing this view on the first startup using NSUserDefaults, but I am having trouble getting the view to display the way I want it to modally, not as the rootViewController. Here is my AppDelegate.m code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];

    self.window.rootViewController = self.viewController;

    BOOL hasShownStartup = [[NSUserDefaults standardUserDefaults] boolForKey:kAppHasShownStartupScreen];

    if (!hasShownStartup) {
        [self showInstructions];
    }

    [self.window makeKeyAndVisible];
    return YES;
}

- (void)showInstructions
{
    NSLog(@"showing instructions");
    InstructionsViewController *howToView = [[InstructionsViewController alloc] initWithNibName:@"InstructionsViewController"
                                                                                     bundle:nil];
    self.instructionsViewController = howToView;
    [howToView release];
    UINavigationController *howtoNavController = [[UINavigationController alloc]
                                             initWithRootViewController:self.instructionsViewController];
    self.instructionsViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self.viewController presentModalViewController:howtoNavController animated:NO];
    [howtoNavController release];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kAppHasShownStartupScreen];
}

I want my rootViewController to remain self.viewController. I want to be able to click 'Done' on the instructionsViewController nav bar to transition back to the rootViewController. Executing the code as written never shows the instructions view. The only way I can see instructionsViewController is if I change the line [self.viewController presentModalViewController:howtoNavController animated:NO]; to self.window.rootViewController = self.instructionsViewController; but this is obviously not what i want (unless I could modally transition back to viewController).

Hopefully I've made it clear enough what I am trying to accomplish. Thanks in advance.

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

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

发布评论

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

评论(2

呆橘 2025-01-13 17:45:48

尝试将 [self showInstructions]; 移动到 applicationDidBecomeActive:

Try moving [self showInstructions]; to applicationDidBecomeActive: instead.

虫児飞 2025-01-13 17:45:48

尝试将 [self showInstructions] 放在 [self.window makeKeyAndVisible] 之后:

[self.window makeKeyAndVisible];

if (!hasShownStartup) {
    [self showInstructions];
}

Try putting the [self showInstructions] after [self.window makeKeyAndVisible]:

[self.window makeKeyAndVisible];

if (!hasShownStartup) {
    [self showInstructions];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文