在第一个应用程序开放时显示教程视图

时间:2014-02-11 14:55:29

标签: ios objective-c cocoa-touch

我正在尝试创建只在首次打开应用时显示的简介/欢迎屏幕。在用户'chroipahtz'here的帮助下,我取得了一些成功,但只能看到黑色视图而不是我欢迎的ViewController。注意:此黑屏会正确关闭我的应用程序的其余部分,并且应用程序运行时没有错误或警告。

screen shot of black screen

我在根视图控制器“viewDidLoad”方法中有以下代码:

if(![[NSUserDefaults standardUserDefaults] boolForKey:@"hasSeenTutorial"])
{
    WelcomeViewController *welcomeController = [[WelcomeViewController alloc] init];
    [self.navigationController pushViewController:welcomeController animated:YES];
}
else{    NSLog(@"Tutorial has been seen");}
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasSeenTutorial"];

显示第一个开口的介绍视图。我在故事板中创建了一个View Controller,并在身份检查器中为其分配了正确的自定义类。

screen shot of storyboard

我已经包含了标题:

#import "WelcomeViewController.h"

如果可以说明为什么欢迎视图控制器界面没有显示带有我在故事板中概述的文本的灰色视图,请提供指导。

3 个答案:

答案 0 :(得分:6)

你需要在“else”的情况下推送另一个视图。下面的代码更加健壮并且使用了故事板。您必须为视图控制器和故事板命名才能使其正常工作:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    if(![[NSUserDefaults standardUserDefaults] dictionaryForKey:@"hasSeenTutorial"]){
        UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"welcomeViewController"];
        self.window.rootViewController = viewController;
    } else {
        UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"nonWelcomeViewController"];
        self.window.rootViewController = viewController;
    }

    [self.window makeKeyAndVisible];

    // Override point for customization after application launch.

    return YES;
}

答案 1 :(得分:1)

如何制作像这样的ContainerViewController

@interface ContainerViewController ()

@property (nonatomic) MainViewController *mainViewController
@property (nonatomic) TutorialViewController *tutorialViewController

@end

@implementation ContainerViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)loadView
{
    [super loadView];

    _mainViewController = [[MainViewController alloc] initWithNibName:nil bundle:nil];
    [self addChildViewController:_mainViewController];
    [self.view addSubview:_mainViewController.view];
    [_mainViewController didMoveToParentViewController:self];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.

    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"hasSeenTutorial"])
    {
        [self showTutorialViewController];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasSeenTutorial"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

- (void)showTutorialViewController
{
    if (!_tutorialViewController) {
        _tutorialViewController = [[TutorialViewController alloc] initWithNibName:nil bundle:nil];
    }

    [self addChildViewController:_tutorialViewController];

    CGRect screenBounds = _mainViewController.view.bounds;
    CGRect fromFrame = CGRectMake(0.0f, screenBounds.size.height, screenBounds.size.width, screenBounds.size.height);
    CGRect toFrame = screenBounds;

    _tutorialViewController.view.frame = fromFrame;
    [self.view addSubview:_tutorialViewController.view];
    [UIView animateWithDuration:0.3f
                          delay:0.0f
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         _tutorialViewController.view.frame = toFrame;
                     } completion:^(BOOL finished){
                         [_tutorialViewController didMoveToParentViewController:self];
                     }];
}

请参阅Apple's document有关“ContainerViewController”的内容。

您可以使用我的库来计算发布次数。 如果需要,请参阅this并尝试。碰巧,我昨天做了。

答案 2 :(得分:0)

您已在故事板中设置,其中的一些视图控制器是WelcomeViewController。但这并不适用于相反的方向。当你初始化WelcomeViewController时,你只有类的实例,它没有连接到你的故事板中的视图控制器。

您需要做的是从故事板中实例化视图控制器,如下所示:

UIStoryboard *storybrd = [UIStoryboard storyboardWithName:@"YourStoryBoardName"                 bundle:nil];
UIViewController *mycontroller =[storybrd instantiateViewControllerWithIdentifier:@"identOfWelcomeViewController"];

当然,您必须在故事板中为视图控制器设置正确的标识符。