views:

842

answers:

3

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?

+5  A: 

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.

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.

NilObject
+1 for giving exactly the parts missing from what I answered :)
Jesse Rusak
+3  A: 

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):

MyView *myView = (MyView*)controller.view;
id myView = controller.view;

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.)

Jesse Rusak
A: 

I think you should rather use @dynamic ... please read: http://stackoverflow.com/questions/1160498/synthesize-vs-dynamic-what-are-the-differences that answer was really helpful to me :)

Ondrej