How not to confuse super(class) and superview in Xcode for iOS

In preparation for a forthcoming blogpost on self, super, polymorphism and inheritance in Xcode and iOS5, which has been inspired and informed by Matt Neuburg's book Programming iOS 5, I'm posting some sample (commented) code to illustrate the points that will be made. If you wish to experiment, then copy and paste this into your Xcode project.


The thing that I wish to illustrate here is that the use of super and superview have two entirely different uses. Something that I had confused for quite a while, and which I've no doubt others will confuse too. This is because while the self keyword references the instance of an object, super is a reference to its superclass never its superview (as one might expect). The best piece of advice that I can give therefore is to always think of super as super(class) in your mind when you type it. In fact if you place the following line of code in you appName-prefix.pch file:


    #define superclass super


then you'll actually be able to replace all instances of super with superclass. So add this define code to the .pch file then try the following code for further clarity.


First of all let's look at the header file for our SquareView subclass, the same one from last time.

#import

@interface SquareView : UIView // SquareView is a subclass of UIView. This means that UIView is its super(class)

-(void)backgroundColorChange;

@end
Now let's use the ViewController.m file to create an instance of SquareView and have it call the single method contained in that subclass:

#import "ViewController.h"

#import "SquareView.h"

@interface ViewController ()

@end
@implementation ViewController : UIViewController

- (void)viewDidLoad

{
[superclass viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

SquareView *squareViewNew = [[SquareView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; // Creates a pointer to an instance of a SquareView object - note initWithFrame is inherited from UIView, which is SquareView's super(class)

[self.view addSubview:squareViewNew]; // Adds squareViewNew as subview to the view controller's view

[squareViewNew backgroundColorChange]; // backgroundColorChange is called, which is a SquareView instance method taken directly from SquareView class

}

@end
We already have the SquareView.h file outlined above, but for this example I want to add a few more elements to the backgroundColorChange: method in SquareView.m

#import "SquareView.h"

@implementation SquareView

- (id)initWithFrame:(CGRect)frame

{
    
    self = [superclass initWithFrame:frame];
    
    if (self) {
        
        // Initialization code
        
    }
    
    return self;
    
}

-(void)backgroundColorChange {
    
    [self setBackgroundColor:[UIColor blueColor]]; // here's the implementation of the method we've created
    
    superclass.alpha=0.8; // in this line of code superclass.alpha has the same result as writing self.alpha would but instead of looking in the SquareView class first and working backwards through the inheritance tree (see previous post) - it goes straight to the super class (i.e. UIView) and looks there. This is useful if the subclass overrides one of the super(class) methods and you need to have access to the method in a non-overridden state
    
    self.superview.alpha=0.5; // as a beginner I often confused reference to the superview - i.e. the view within which the current one is nested or contained - and the word super. They are however two different things. Any instance of the SquareView class that calls this method will have the alpha of the view within which it is placed changed to 0.5 whereas if we wrote superclass.self.alpha=0.5; or superclass.alpha=0.5;then it would be the transparency of the SquareView instance itself that would be changed
    
}

/*

 // Only override drawRect: if you perform custom drawing.

 // An empty implementation adversely affects performance during animation.

 - (void)drawRect:(CGRect)rect

 {

 // Drawing code

 }

 */

@end
Resist the temptation to skim over the lengthy comments in SquareView.m, they are there for a reason. I also suggest you try out your understanding of the concept now in Xcode.

Comments