如何添加一个新的 StoryBoard,并将其设置为没有错误的 Main StoryBoard xCode

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18480163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:44:24  来源:igfitidea点击:

How to Add a New StoryBoard, and set it to become the Main StoryBoard xCode without error

iosxcodeuiviewcontrollerstoryboard

提问by Confused_person

Currently, I have a working app with a single storyBoard. The working app is based on master-detail view layout. I decided to add a new storyboard, and set it to become the main storyboard. When I click run, it will run and display a blank page, which is what I expected as I have not added any view controllers.

目前,我有一个带有单个故事板的工作应用程序。工作应用程序基于主从视图布局。我决定添加一个新的故事板,并将其设置为主故事板。当我点击运行时,它会运行并显示一个空白页面,这是我所期望的,因为我没有添加任何视图控制器。

I am still new to objective-C, as such, there are many things that I still do not understand, Here comes the interesting part.

我还是Objective-C的新手,因此,还有很多我仍然不明白的地方,有趣的部分来了。

When I add a UIViewController(and associated it to a subclass of UIViewController) inside my newly changed Main storyboard, and then tried to run it, it displays an error.

当我在新更改的主故事板中添加 UIViewController(并将其关联到 UIViewController 的子类),然后尝试运行它时,它显示错误。

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController viewControllers]: unrecognized selector sent to instance 0x7569a80'

Under my AppDelegate:

在我的 AppDelegate 下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
splitViewController.delegate = (id)navigationController.topViewController;

return YES;
}

I am not so sure what do I need to add to the code above. I did try the below line without success too,

我不太确定我需要在上面的代码中添加什么。我也尝试过下面的代码但没有成功,

UIViewController *viewController = (UIViewController *)self.window.rootViewController;

As such, how do I solve this problem?

因此,我该如何解决这个问题?

回答by preetam

Take storyboard file from New file-->ios-->user interface---> storyboard

从中获取故事板文件 New file-->ios-->user interface---> storyboard

create @propertystoryboard in appdelegate like

@property在 appdelegate 中创建故事板,例如

@property (nonatomic, retain) UIStoryboard* storyboard ;

before implementation in .m file write:

在 .m 文件中实现之前写:

@interface AppDelegate ()
@property (strong, nonatomic) UIViewController *initialViewController;
@end

This is to take refrence of initial viewcontroller.

这是为了参考初始视图控制器。

then @synthesize storyboardin appdelegate.m and then for navigation you can write in didFinishLaunchingWithOptions

然后@synthesize storyboard在 appdelegate.m 然后导航你可以写didFinishLaunchingWithOptions

 //init storyboard
storyboard = nil;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0"))
{

    CGSize result = [[UIScreen mainScreen] bounds].size;
    if(result.height == 480)
    {
       storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhoneIOS5" bundle:nil];

        NSLog(@"Version < 6");
        // iPhone Classic
    }
    if(result.height == 568)
    {
        // iPhone 5
      storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
        NSLog(@"Version 6");
    }




    //NSLog(@"IOS 6");
}
else
{
    storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhoneIOS5" bundle:nil];
     //NSLog(@"IOS 5");
}
self.initialViewController = [storyboard instantiateInitialViewController];

//instantiateInitialViewControllermeans it returns the arrow pointed view-controlleron storyboard that is root view controller.

//instantiateInitialViewController意味着它返回故事板上箭头指向的视图控制器,即根视图控制器。

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

回答by Sherif Aziz

On Xcode 7 you can go to info.plist and set the key "Main storyboard file base name" to the name of your new Storyboard .

在 Xcode 7 上,您可以转到 info.plist 并将键“Main storyboard file base name”设置为新 Storyboard 的名称。

Example: You create a new storyboard file by going to file -> new -> file -> Storyboard file and you save it as "MyAwesomeUI.storyboard" then in info.plist you change the key of "Main storyboard file base name" to "MyAwesomeUI" .

示例:您通过转到文件 -> 新建 -> 文件 -> 故事板文件创建一个新的故事板文件,并将其另存为“MyAwesomeUI.storyboard”,然后在 info.plist 中将“主故事板文件基本名称”的键更改为“MyAwesomeUI”。

Cheers!

干杯!