Swift:对UIViewController的根视图进行子类化

Ale*_*son 21 uiviewcontroller uiview ios swift

tl; dr:我怎么能告诉Swift我的子类属于覆盖MyViewControllerview属性UIView


我想做什么

我非常喜欢UIViewUIViewController视图提供子类.例如:

// MyView --------------------------------------------------------

@interface MyView: UIView
@property (nonatomic, strong) UITableView *tableView;    
@end

@implementation MyView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _tableView = [[UITableView alloc] initWithFrame:frame];
    }
    return self;
}

@end

// MyViewController ----------------------------------------------

@interface MyViewController: UIViewController <UITableViewDataSource>
@property (nonatomic, retain) MyView *view;
@end

@implementation MyViewController

- (void)loadView {
    self.view = [[MyView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.tableView.dataSource = self;
    // etc.
}

@end
Run Code Online (Sandbox Code Playgroud)

这很好,因为它将视图创建和布局逻辑与视图控制器分开.好的.

我可以想象,这就像这样转换成Swift:

// MyView --------------------------------------------------------

class MyView: UIView {
    let tableView: UITableView!

    init(frame: CGRect) {
        super.init(frame: frame)
        tableView = UITableView(frame: frame)
    }
}

// MyViewController ----------------------------------------------

class MyViewController: UIViewController, UITableViewDataSource {
    override func loadView() {
        view = MyView(frame: UIScreen.mainScreen().bounds)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // this causes the compiler to complain with:
        // 'UIView' does not have a member named 'tableView'
        self.view.tableView.dataSource = self
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是我似乎无法弄清楚如何告诉视图控制器它view是一个实例MyView而不是UIView它自己.

尝试失败

这是我到目前为止所尝试的:

我在顶部试过这个MyViewController,出现以下错误:

override var view: MyView!
// error: Cannot override mutable property 'view' of
//        type 'UIView' with covariant type 'MyView!'
Run Code Online (Sandbox Code Playgroud)

我试过这个loadView,但没有运气:

view = MyView(frame: UIScreen.mainScreen().bounds) as MyView
// this produces the same error as in the original code:
// 'UIView' does not have a member named 'tableView'
Run Code Online (Sandbox Code Playgroud)

所以这就是问题所在

我怎么能告诉Swift我用一个子类覆盖了它MyViewControllerview属性MyView?这甚至可能吗?如果没有,为什么不呢?

Nat*_*ook 24

我认为在Swift中可能无法实现完全相同的实现.从Swift书中有关覆盖属性的部分:

"您必须始终声明要覆盖的属性的名称和类型,以使编译器能够检查您的覆盖是否与具有相同名称和类型的超类属性匹配."

但是,您可以使用一个计算属性来返回视图控制器view属性的类型转换版本,它几乎一样干净:

class MyViewController: UIViewController, UITableViewDataSource {
    var myView: MyView! { return self.view as MyView }

    override func loadView() {
        view = MyView(frame: UIScreen.mainScreen().bounds)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.myView.tableView.dataSource = self
    }
}
Run Code Online (Sandbox Code Playgroud)