gpt4 book ai didi

ios - 如何添加新 StoryBoard,并将其设置为 Main StoryBoard xCode 而不会出错

转载 作者:行者123 更新时间:2023-12-01 17:20:45 30 4
gpt4 key购买 nike

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

我对objective-C还是新手,因此,还有很多东西我还是不明白,有趣的部分来了。

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

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

在我的 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;
}

我不太确定我需要在上面的代码中添加什么。我也尝试过以下行但没有成功,
UIViewController *viewController = (UIViewController *)self.window.rootViewController;

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

最佳答案

New file-->ios-->user interface---> storyboard 获取 Storyboard文件

创建 @property appdelegate 中的 Storyboard

@property (nonatomic, retain) UIStoryboard* storyboard ;

在 .m 文件中执行之前写入:
@interface AppDelegate ()
@property (strong, nonatomic) UIViewController *initialViewController;
@end

这是为了引用初始 View Controller 。

然后 @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];

// instantiateInitialViewController表示它返回 箭头指向的 View Controller 在作为 Root View Controller 的 Storyboard上。
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = self.initialViewController;
[self.window makeKeyAndVisible];

关于ios - 如何添加新 StoryBoard,并将其设置为 Main StoryBoard xCode 而不会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18480163/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com