I have a custom UIViewController and custom UIView. I'd like to override the viewcontroller.view property to return MyCustomUIView.
Right now I have:
@interface MyViewController : UIViewController {
IBOutlet MyView* view;
}
@property (nonatomic, retain) IBOutlet MyView* view;
This compiles but I get a warning: property 'view' type does not match super class 'UIViewController' property type.
How do I alleviate this warning?
@lpaul7 already posted a link to Travis Jeffery's blog as a comment, but it's so much more correct than all the other answers that it really needs the code to be an answer:
ViewController.h:
@interface ViewController : UIViewController
@property (strong, nonatomic) UIScrollView *view;
@end
ViewController.m:
@implementation ViewController
@dynamic view;
- (void)loadView {
self.view = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
}
@end
If you using a xib, instead of overriding loadView
change the type of your view controller's root view and add IBOutlet
to the property.
See Overriding UIViewController's View Property, Done Right for more details.
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:
Objective-C does not allow return type covariance.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With