【问题标题】:How to deal with storyboard instead of xib file in such case?在这种情况下如何处理故事板而不是 xib 文件?
【发布时间】:2013-01-05 10:52:03
【问题描述】:

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

- (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];

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

知道出了什么问题吗?

【问题讨论】:

  • 为什么要投反对票?这个问题有什么问题...

标签: objective-c uiviewcontroller ios6 appdelegate


【解决方案1】:

我只是在 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];

【讨论】:

  • 我认为自从添加故事板以来,创建更少代码的趋势更加明显。也就是说,我会在 Info.plist 文件中为您正在使用的目标声明Main storyboard file base name(又名 UIMainStoryboardFile)。然后我会创建一个链接到视图(您的启动屏幕)的类,以动画出视图并将新视图推送给用户。也许你会更喜欢这个实现?只是一个选择...
【解决方案2】:

我认为这是因为您正在分配新的 MyBookViewController。相反,您必须提供情节提要的参考,在上面您有 MyBookViewController 的 UI 设计,就像这样

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

确保 ViewController 标识符

更新1:

试试

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

【讨论】:

  • 我也试过这个。但我得到错误property storyboard not found on object of type MyBookAppDelegate.m
  • 当我有MyBookViewController *myNew=self.window.rootViewController;时仍然是同样的黑屏
  • @user1996238;创建一个新的视图控制器并在 Viewdidload 中添加带有故事板参考的代码
  • 但我怎样才能接触到新的视图控制器?
  • @user1996238: 将新的 viewController 分配给 rootviewController
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-06
相关资源
最近更新 更多