本文介绍了如何覆盖“视图"?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;

这可以编译但我收到警告:属性view"类型与超类UIViewController"属性类型不匹配.

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

我该如何缓解这个警告?

How do I alleviate this warning?

推荐答案

简短的回答是你没有.原因是属性实际上只是方法,如果您尝试更改返回类型,则会得到:

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 不允许返回类型协变.

Objective-C does not allow return type covariance.

您可以做的是添加一个新属性myView",并使其简单地对view"属性进行类型转换.这将减少整个代码的类型转换.只需将您的视图子类分配给视图属性,一切都会正常工作.

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 中的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:31