JamesB41
JamesB41

Reputation: 703

Need to cast self.view in UIViewController?

I've created a UIViewController which contains a view with a custom class that I've built. I've connected the view to the File's Owner view outlet. It renders properly and I don't have any real problems. However, I want to call a method within my view from my view controller instance, say it's called drawSomething. So within a method in my controller, I use:

[self.view drawSomething];

It works. But I get a pre-compiler warning because it appears to not know the methods within my custom view definition. Is my approach wrong here? I can create a local variable and cast it to make the warning go away obviously. I've imported the view's header file, so that's not the issue.

I feel like I'm missing something trivial.

.h of my custom view contains:

-(void) drawSomething;

.m of my custom view contains:

-(void) drawSomething { <code> };

Upvotes: 3

Views: 4180

Answers (1)

Evan Mulawski
Evan Mulawski

Reputation: 55334

self.view is basically a pointer to a UIView used as the main view for the view controller. Since your view is a custom class with custom methods, you need a cast to your custom class to call your custom methods:

[((MyCustomClass *)self.view) drawSomething];

Otherwise, you are calling drawSomething on a standard UIView, which does not exist.

Upvotes: 12

Related Questions