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?
The UIViewController's view property/method will return your view automatically. I think you'll just have to cast the result to MyView (or just use the id type):
I think the code you have posted above will cause you trouble. You probably don't want to create a view member yourself (because then the controller will be storing 2 views, and might not use the right one internally) or override the view property (because UIViewController has special handling for it.) (See this question for more info about the latter.)
Sorry, but your short answer is wrong.
The correct implementation of this would be to add a @property to the header file with your return type. Then instead of @synthesize you add the getter and setter manually and return a cast type from [super view]
for instance my class is a PlayerViewController
PlayerViewController.h
PlayerViewController.m
the important thing is to put the correct class into the view.
Putting a UIView where a PlayerView goes would likely work in the Interface Builder, but would not function correctly in the code.
I am currently using this implementation.
@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:
ViewController.m:
If you using a xib, instead of overriding
loadView
change the type of your view controller's root view and addIBOutlet
to the property.See Overriding UIViewController's View Property, Done Right for more details.
If anyone is on Swift 2.0+, you can use protocol extensions for a reusable implementation:
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.