views:

63

answers:

1

I have a class that inherits from UIView

However when trying to use the follow (UIView *)viewWithTag:(NSInteger)tag I get the warning: *incompatible Objective-C types initializing 'struct UIView *', expected 'struct CustomView '

Right now I have made a custom method that returns my custom view, the method uses a for loop with view.subviews since the views I am looking for come from the same superview. I do a conditional check if the tag matches the one I am searching for then I return the view.

I suppose I am just wondering if there is a better practice to this?

Thanks

+2  A: 

You're probably doing:

CustomView * aView = [someView viewWithTag:42];

viewWithTag: returns a UIView, not a CustomView. There are a couple ways around this:

  1. Casting. If you are absolutely sure that you'll only ever get a CustomView by doing this, then you can do:

    CustomView * aView = (CustomView *)[someView viewWithTag:42];
  2. Reflection. If you're not sure that you're going to get a custom view, then assign it into a UView reference and decide later what to do with it:

    UIView * aView = [someView viewWithTag:42];
    if ([aView isKindOfClass:[CustomView class]]) {
      CustomView * customView = (CustomView *)aView;
    }
Dave DeLong
Thanks, that's worked. I know it will always return the custom class however the additional bit may help at some point
Chris