在这种情况下如何处理故事板而不是xib文件?

时间:2013-01-22 08:50:20

标签: objective-c uiviewcontroller ios6 appdelegate

我有一个例子,我在应用程序启动时有动画。在开始时显示图像,然后从右向左移动,然后消失。以下是我的代码。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewwController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewwController;
    [self.window makeKeyAndVisible];


    //add the image to the forefront...
    UIImageView *splashImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [splashImage setImage: [UIImage imageNamed:@"Default"]];
    [self.window addSubview:splashImage];
    [self.window bringSubviewToFront:splashImage];


    //set an anchor point on the image view so it opens from the left
    splashImage.layer.anchorPoint = CGPointMake(0, 0.5);

    //reset the image view frame
    splashImage.frame = CGRectMake(0, 0, 320, 480);

    //animate the open
    [UIView animateWithDuration:1.0
                          delay:0.6
                        options:(UIViewAnimationCurveEaseOut) 
                     animations:^{

                         splashImage.layer.transform = CATransform3DRotate(CATransform3DIdentity, -M_PI_2, 0, 1, 0);
                     } completion:^(BOOL finished){

                         //remove that imageview from the view
                         [splashImage removeFromSuperview];
                     }];

    return YES;

}

我想要的是使用故事板来做到这一点,所以我改变了如下代码。

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MyBookViewController *myNew = [[MyBookViewController alloc] init];
self.myBookViewController = myNew;
self.window.rootViewController = self.myBookViewController;
[self.window makeKeyAndVisible];

但是我没有看到我的viewcontroller(MyBookViewController)。我只看到图像从右向左移动,然后是黑屏。

知道出了什么问题吗?

2 个答案:

答案 0 :(得分:3)

我是在this SO question的帮助下自己做的。

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                         bundle: nil];
MyBook001ViewController *controller = (MyBook001ViewController*)[mainStoryboard
                                                                             instantiateViewControllerWithIdentifier: @"firstStory"];
self.window.rootViewController = controller;
[self.window makeKeyAndVisible];

答案 1 :(得分:0)

我认为这是因为你正在分配新的MyBookViewController。相反,你必须提供故事板的参考,你有你的MyBookViewController的UI设计,像这样

MyBookViewController *myNew=[self.storyboard instantiateViewControllerWithIdentifier:@"MyBookViewController"];

确保ViewController标识符

UPDATE1:

尝试

MyBookViewController *myNew=[self.window.rootViewController];