How do I override the "view" property in UIViewController?

14,457

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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

@property (strong, nonatomic) IBOutlet PlayerView *view;

PlayerViewController.m

- (PlayerView *)view{
    return (PlayerView*)[super view];
}
- (void)setView:(PlayerView *)view{
    [super setView:view];
}

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.

Share:
14,457
Keith Fitzgerald
Author by

Keith Fitzgerald

Updated on June 08, 2022

Comments

  • Keith Fitzgerald
    Keith Fitzgerald almost 2 years

    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?