hakkurishian
hakkurishian

Reputation: 276

Getting a NSViewController's View if it is a custom class?

I use the following code to get my View out of my controller:

CollectionItemView *myView = [self view]; 

This works pretty well, but I get the warning Incompatible pointer types initializing CollectionItemView __strong with an expression of type NSView. I understand why i get this but is it okay to ignore it or should I overwrite the view property ?

chuck

Upvotes: 5

Views: 296

Answers (2)

Hermann Klecker
Hermann Klecker

Reputation: 14068

There is no need to overwrite it. troolee already suggested two working solutions. However, just to be save I'd rather code it differently.

CollectionItemView *myView = nil;
if ([[self view] isKindOfClass:[CollectionItemView class])
  self.view = (CollectionItemView*)[self view];

The shorter version without isKindOfClass test is ok when you know for sure from the context that the object must be of type CollectionItemView or any of its subclasses.

Upvotes: 0

Pavel Reznikov
Pavel Reznikov

Reputation: 3208

If you are sure that [self view] is CollectionItemView just do:

CollectionItemView *myView = (CollectionItemView*)[self view];

or (which is better) you can use:

id myView = [self view];

Upvotes: 2

Related Questions