我想在 Storyboard ViewController 中显示以编程方式创建的 ViewController。 Storyboard 的 ViewController 与“以编程方式”创建的 ViewController 属于不同的类。
我有以下类(class):
ViewController (linked to storyboard scene, and implementation happens here)
OnboardingVC (all elements are created over here)
我试过以下方法:
-(id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if(self){
self.onboardVC = [self generateFirstDemoVC]; // returns in an instance ofOnboardingViewController
self = (ViewController*)self.onboardVC;
}
return self;
}
这(显然)崩溃了。
我不想要的是:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:self.onboardVC];
[self.window makeKeyAndVisible];
因为这没有考虑 Storyboard 中的设置,我不希望这样,因为 VC 需要由 NavigationController
管理。我该如何实现?
最佳答案
听起来您有一个 UINavigationController 作为 Storyboard的入口点。您可以在应用程序启动时操作此导航 Controller ,以添加、删除或替换 View Controller 等。
例如:
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
UIViewController* initialVc = [OnboardingViewController generateFirstDemoVC]; // or whatever
UINavigationController* nav = (id)_window.rootViewController;
nav.viewControllers = @[ initialVc ];
return YES;
}
关于ios - 为 Storyboard VC 初始化程序化 VC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27161344/