Subclassing a subclass - question regarding IBOutlets and IBActions

Discussion in 'iOS Programming' started by moonman239, Sep 27, 2013.

  1. moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    If I have a class that is a subclass of a subclass, and I place an IBOutlet or IBAction in the former class, and it has the same name as an IBOutlet or IBAction in the latter class, will iOS treat them as equivalent?
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    Outlets and actions are a little different.

    An action is a method and there's really nothing different about it from any other method. So the normal rules of overriding apply. If a base class and a derived class both implement the same method the derived class's implementation will be called. The derived class's implementation may or may not call the base class's version.

    If you're following the recommendations of how to work with instance variables you are declaring them as @properties only. An IBOutlet is not different from other @properties. Since an @property is a method the normal rules of overriding apply here too. The difference has to do with the underlying instance variable. The two @properties may use the same underlying ivar or different ivars. You can control this with @synthesize. You can also control the behavior with having the derived class call the base class @property, or not. There are some delegates in UIKit that are affected by this. UITableView and UIScrollView both have a delegate @property and of course UITableView is a subclass of UIScrollView. Are there two delegate ivars or one? If you assign the delegate property to a UITableView the same value is also set to the base class's delegate property. That seems to be a sensible way to do this in this case.

    I ran into this issue yesterday. I have a UITableViewCell subclass that I wrote several years ago. It overrides a couple of the base class @properties for subviews but it creates its own subviews. The @synthesize statements specify different names for the ivars than the base class does and the @properties don't call super. Something like this

    @synthesize textLabel = _MyTextLabel;

    Without the @synthesize statement the code wouldn't work because the @property would use the base class textLabel, which isn't what I want.
     

Share This Page