从欢迎屏幕推送视图控制器时遇到问题[关闭]

Posted

技术标签:

【中文标题】从欢迎屏幕推送视图控制器时遇到问题[关闭]【英文标题】:Trouble pushing view controller from a welcome screen [closed] 【发布时间】:2014-02-08 19:32:55 【问题描述】:

我有一个欢迎屏幕,仅在用户第一次打开应用程序时显示。屏幕运行良好,但是当用户单击完成时,我无法让它显示正常屏幕。

这是应用程序委托中创建普通屏幕的代码 -

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;

    if([[NSUserDefaults standardUserDefaults] boolForKey:@"TermsAccepted"]!=YES)
    
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"TermsAccepted"];
    

    // Override point for customization after application launch.
    FeedViewController *feedViewController = [[FeedViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc]  initWithRootViewController:feedViewController];
    [self.window addSubview:nav.view];
    self.window.rootViewController = nav;

    [self.window makeKeyAndVisible];

    self.tabBarController = [[UITabBarController alloc] init];
    [[UITabBar appearance] setTintColor:[UIColor redColor]];


    // FeedViewController
    feedViewController=[[FeedViewController alloc] init];
    feedViewController.tabBarItem.image=[UIImage imageNamed:@"Describe-Home_Icon_NormalArtboard-1"];
    feedViewController.title = @"Timeline";
    feedViewController.tabBarItem.title = nil;

    //TodayViewController
    TodayViewController *todayViewController = [[TodayViewController alloc] init];
    todayViewController.tabBarItem.image = [UIImage imageNamed:@"Today_Icon"];
    todayViewController.title = @"Today";
    todayViewController.tabBarItem.title = nil;

    //CreateViewController
    self.createViewController = [[CreateViewController alloc] init];
    self.createViewController.tabBarItem.image = [UIImage imageNamed:@"Create_Icon"];
    self.createViewController.title = @"Create";
    self.createViewController.tabBarItem.title = nil;

    //AlertViewController
    AlertsViewController *alertsViewController = [[AlertsViewController alloc] init];
    alertsViewController.tabBarItem.image=[UIImage imageNamed:@"Alerts_IconArtboard-1"];
    alertsViewController.title=@"Alerts";
    alertsViewController.tabBarItem.title = nil;

    //ProfileViewController
    ProfileViewController *profileViewController = [[ProfileViewController alloc] init];
    profileViewController.tabBarItem.image=[UIImage imageNamed:@"Profile_IconArtboard-1"];
    profileViewController.title=@"Profile";
    profileViewController.tabBarItem.title = nil;

    NSMutableArray *tabBarViewControllers = [[NSMutableArray alloc] initWithCapacity:2];

    self.tabBarController = [[UITabBarController alloc] init];


    UINavigationController *feedNavigationController = [[UINavigationController alloc] initWithRootViewController:feedViewController];
    [tabBarViewControllers addObject:feedNavigationController];
    feedNavigationController = nil;

    UINavigationController *todayNavigationController = [[UINavigationController alloc] initWithRootViewController:todayViewController];
    [tabBarViewControllers addObject:todayNavigationController];
    todayNavigationController = nil;

    UINavigationController *createNavigationController = [[UINavigationController alloc] initWithRootViewController:self.createViewController];
    [tabBarViewControllers addObject:createNavigationController];
    createNavigationController = nil;

    UINavigationController *alertsNavigationController = [[UINavigationController alloc] initWithRootViewController:alertsViewController];
    [tabBarViewControllers addObject:alertsNavigationController];
    alertsNavigationController = nil;

    UINavigationController *profileNavigationController = [[UINavigationController alloc] initWithRootViewController:profileViewController];
    [tabBarViewControllers addObject:profileNavigationController];
    profileNavigationController = nil;

    self.tabBarController.viewControllers = tabBarViewControllers;
    tabBarViewControllers = nil;

    [self.window addSubview:self.tabBarController.view];

    return YES;

在 feedViewController 中推送欢迎视图控制器 -

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"TermsAccepted"])
    NSLog(@"Second time opening the app");

else
    WelcomeViewController *welcomeViewController = [[WelcomeViewController alloc] init];
    [self.navigationController pushViewController:welcomeViewController animated:NO];

返回不起作用的提要 -

-(void)showDone:(UIButton *)sender 

    if (self.navigationItem.rightBarButtonItem.tintColor == [UIColor redColor]) 
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"TermsAccepted"];

        FeedViewController *feedViewController = [[FeedViewController alloc] init];
        [[UIApplication sharedApplication] keyWindow].rootViewController = feedViewController;

        self.tabBarController.tabBar.hidden = NO;
        self.navigationController.navigationBar.hidden = NO;
    

【问题讨论】:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 不是为容纳初始化应用程序所需的所有垃圾而设计的槽。 【参考方案1】:

看起来当您显示欢迎屏幕时,您将其推送到UINavigatonController。但是当您单击完成时,您会尝试在窗口上设置根视图控制器,而不是仅仅从导航控制器中弹出视图控制器。看起来您正在创建 FeedViewController 的新实例,而不是使用您已经创建的实例。

另外,您是否检查过它是否正在执行您的showDone: 方法中的代码?您正在使用==UIBarButtonItemtintColorUIColor 进行比较,但使用== 只会在它们都是完全相同的UIColor 实例时返回true,它们可能不会是。您需要使用方法isEqual: 来比较两种颜色,因此您可以执行以下操作:

[self.navigationItem.rightBarButtonItem.tintColor isEqual:[UIColor redColor]]

请注意,如果相同的颜色位于不同的颜色空间中,这不会总是返回 YES,但大多数时候这应该可以工作。

此外,您应该考虑将代码移出应用委托,因为通常application:didFinishLaunchingWithOptions: 仅用于需要在启动时立即完成的事情。它不应该用于初始化一堆视图控制器,它们应该只在需要显示它们时才被初始化。

【讨论】:

以上是关于从欢迎屏幕推送视图控制器时遇到问题[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

如何从视图控制器推送 xib

推送视图控制器后导航控制器隐藏

UITableView推送segue多次触发

如何将视图控制器从视图控制器推送为全屏幕,呈现为半模式

推送视图控制器时,导航栏上出现 iOS 11 黑条

从视图控制器显示(例如推送)到导航控制器有效?!为啥?