swift 3 - 未显示子类视图控制器

时间:2017-03-03 20:33:38

标签: objective-c inheritance swift3 subclass viewcontroller

在我的Swift 3 / Obj-C应用程序中,我无法显示一个快速视图控制器,它是和Obj-C类的子类。

以下代码完美无缺:

NavigationController.swift

func myMethod(){
    ...
    let vc = ParentViewController.init(someParameter: parameter) // << results in desired screen
    vc?.stringProperty = "someString"
    self.pushViewController(vc!, animated: true)
}

SuperParentViewController.h

@interface SuperParentViewController : UIViewController 
     @property (nonatomic, strong) NSString *stringProperty;
     - (instancetype)initWithSomeParameter:(SomeClass *)someParameter;
@end

ParentViewController.h

@interface ParentViewController : SuperParentViewController
     // numerous properties are declared here
@end

ParentViewController.m

@implementation ParentViewController
    ...

    - (void)viewDidLoad{
        [super viewDidLoad];
        // a bunch of properties are set
    }

    - (void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
        // more properties are set
    }

    ...
@end

但是,如果我将ParentViewController子类化并尝试呈现子类“SubclassViewController”,则会生成仅显示导航栏项的屏幕;屏幕的其余部分为空白:

NavigationController.swift

func myMethod(){
    ...
    let vc = SubClassedViewController.init(someParameter: parameter) // << results in blank screen
    vc?.stringProperty = "someString"
    self.pushViewController(vc!, animated: true)
}

SubClassedViewController.swift

class SubClassedViewController: ParentViewController
{
    // nothing in this file yet
}

有人可以告诉我为什么SubClassedViewController无法显示ParentViewController的相同内容吗?

1 个答案:

答案 0 :(得分:0)

问题是ParentViewController与.xib文件相关联(单击.xib文件 - &gt;转到识别检查器 - &gt;在自定义类下,您将找到“ParentViewController”)。因此,当子类化ParentViewController来创建SubClassedViewController类时,我还需要使用以下代码来初始化相同的.xib文件:

(Swift 3)

class SubClassedViewController: ParentViewController
{    
    override init(someParameter: someClass) {
        super.init(nibName: "ParentViewController", bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}