用于Storyboard VC的Init编程VC

时间:2014-11-27 00:43:32

标签: ios objective-c uiviewcontroller

我想在故事板ViewController中显示以编程方式创建的ViewController。故事板的ViewController是一个不同的类,因为编程方式'创建了ViewController。

我有以下课程:

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

因为这不考虑故事板中的设置,我不想要这样,因为VC需要由NavigationController管理。我该如何做到这一点?

1 个答案:

答案 0 :(得分:1)

听起来你有一个UINavigationController作为故事板的入口点。您可以在应用程序启动时添加,删除或替换视图控制器等操作此导航控制器。

例如:

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions 
{
    UIViewController* initialVc = [OnboardingViewController generateFirstDemoVC]; // or whatever

    UINavigationController* nav = (id)_window.rootViewController;
    nav.viewControllers = @[ initialVc ];

    return YES;
}