如何覆盖“视图”属性在UIViewController? [英] How do I override the "view" property in UIViewController?

查看:142
本文介绍了如何覆盖“视图”属性在UIViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义UIViewController和自定义UIView。我想重写viewcontroller.view属性返回MyCustomUIView。

I have a custom UIViewController and custom UIView. I'd like to override the viewcontroller.view property to return MyCustomUIView.

现在我有:

@interface MyViewController : UIViewController {    
    IBOutlet MyView* view;
}

@property (nonatomic, retain) IBOutlet MyView* view;

编译但是我收到一个警告:property'view'type does not match super class'UIViewController'属性类型。

This compiles but I get a warning: property 'view' type does not match super class 'UIViewController' property type.

如何缓解此警告?

推荐答案

简单的答案是你不会。原因是属性只是方法,如果你尝试改变返回类型,你会得到:

The short answer is that you don't. The reason is that properties are really just methods, and if you attempt to change the return type, you get this:


  • (UIView *) View;

  • (MyView *)view;

允许返回类型协方差。

Objective-C does not allow return type covariance.

你可以做的是添加一个新的属性myView,并使它简单类型的视图属性。这将缓解你的代码中的类型。只需将视图子类分配给视图属性,一切都应该可以正常工作。

What you can do is add a new property "myView", and make it simply typecast the "view" property. This will alleviate typecasts throughout your code. Just assign your view subclass to the view property, and everything should work fine.

这篇关于如何覆盖“视图”属性在UIViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆