c_cpp 视图控制器支持嵌套的故事板加载

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 视图控制器支持嵌套的故事板加载相关的知识,希望对你有一定的参考价值。

//
//  Copyright © 2013 Yuri Kotov
//

#import "ADVPlaceholderViewController.h"

static void * ObservationContext = &ObservationContext;

@implementation ADVPlaceholderViewController

#pragma mark - NSCoding
- (id) awakeAfterUsingCoder:(NSCoder *)decoder
{
    __autoreleasing NSString *identifier;
    __autoreleasing NSString *storyboardName;
    [self getStoryboard:&storyboardName andIdentifier:&identifier];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:self.nibBundle];
    UIViewController *controller = identifier
        ? [storyboard instantiateViewControllerWithIdentifier:identifier]
        : [storyboard instantiateInitialViewController];

    NSString *keyPath = NSStringFromSelector(@selector(storyboard));
    [controller addObserver:self.observer
                 forKeyPath:keyPath
                    options:NSKeyValueObservingOptionOld
                    context:ObservationContext];

    return controller;
}

#pragma mark - NSKeyValueObserving
+ (void) observeValueForKeyPath:(NSString *)keyPath
                       ofObject:(id)object
                         change:(NSDictionary *)change
                        context:(void *)context
{
    if (ObservationContext == context)
    {
        UIStoryboard *storyboard = change[NSKeyValueChangeOldKey];
        [object removeObserver:self.observer forKeyPath:keyPath];
        [object setValue:storyboard forKey:keyPath];
    }
}

#pragma mark - ADVPlaceholderViewController
- (id) observer
{
    return self.class;
}

+ (id) observer
{
    return self;
}

- (void) getStoryboard:(NSString **)storyboard andIdentifier:(NSString **)identifier
{
    enum NSUInteger { ComponentStoryboard, ComponentIdentifier };
    NSArray *components = [self.title componentsSeparatedByString:@"."];
    switch (components.count)
    {
        case 2:
            *identifier = components[ComponentIdentifier];
        case 1:
            *storyboard = components[ComponentStoryboard];
        break;
        default:
            NSAssert(NO, @"Invalid 'title' format");
        break;
    }
}

@end
//
//  Copyright © 2013 Yuri Kotov
//

#import <UIKit/UIKit.h>

@interface ADVPlaceholderViewController : UIViewController
@end

以上是关于c_cpp 视图控制器支持嵌套的故事板加载的主要内容,如果未能解决你的问题,请参考以下文章

故事板 Segue 未加载视图

故事板加载时首先调用啥方法?

故事板无法加载

如何从位于故事板上的视图控制器获取子视图

如何从故事板中绘制的现有视图控制器构建导航控制器?

试图将苹果的页面控件示例合并到故事板视图控制器中