4

In objective-C should I call the super view override method at the top or the bottom of the method? Whats the difference?

For example:

At the top of the method:

 - (void)viewDidLoad {
// HERE
     [super viewDidLoad];

     //Init the table view
     UITableView *aTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 100, 400)];
     aTableView.delegate = self;
     aTableView.dataSource = self;
     aTableView.backgroundColor = [UIColor clearColor];

     self.tableView = aTableView;
     [aTableView release];
 }

Or at the bottom of the method:

- (void)viewDidLoad {

    //Init the table view
    UITableView *aTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 100, 400)];
    aTableView.delegate = self;
    aTableView.dataSource = self;
    aTableView.backgroundColor = [UIColor clearColor];

    self.tableView = aTableView;
    [aTableView release];

// HERE
    [super viewDidLoad];
}
nevan king
  • 112,709
  • 45
  • 203
  • 241
Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162

3 Answers3

5

In the case of view lifecycle you should call it first in the method because you want the super class to finish the setup before you do what you need.

Although in the case of dealloc you should call super at the end of the method because you want to cleanup before the super class cleans up.

Markus Persson
  • 1,103
  • 9
  • 21
0

As I understand it where you place it would depend on if you need things done in the superclass method before you do what you need to do in your method. So if there is any work that needs to be done first in the supermethod then put the super call at the top.

DirectX
  • 922
  • 10
  • 14
0

To answer this question you need to know why you're calling super in the first place. This is not always obvious with UIKit classes since you can't easily figure out what the methods do internally.

Ignoring that, however, the placement of the super call depends entirely on what the super class implementation does. There is non golden rule here. Sometimes it should go in the top, sometimes bottom and sometimes it's even appropriate to call it in between other rows.

Top

An example on when it should be placed in the top is if you were to override the loadView method of UIViewController to add some subviews to its view. In this case, the superclass implementation loads the actual view that you're supposed to place subviews on, so placing it in the top is the right way to go (or it won't work at all).

Bottom

A pretty common case to place it in the bottom would be if you override viewWillAppear: of UITableViewController. UITableViewController internally calls [self.tableView deselectRowAtIndexPath:animated:] in this method to get the little effect of a row selection fading out when you return to the table view from another view. If you need to do something with the selected row before it's automatically deselect, you must place the super call in the bottom (or below the row where you access the selected indexPath).

Community
  • 1
  • 1
Accatyyc
  • 5,798
  • 4
  • 36
  • 51