Is there a special way to declare methods that need to be known about but not actually used within the class they're declared in?

StackOverflow https://stackoverflow.com/questions/23450403

Question

This button is declared in my base class:

{
    // Setup done button for tool bar
    UIButton *doneButton = [[UIButton alloc] initWithFrame:CGRectMake(170, 3, 110, 38)];
    [doneButton setBackgroundColor:[UIColor blackColor]];
    [doneButton setTitle:@"Done" forState:UIControlStateNormal];
    [doneButton addTarget:_thisController action:@selector(doneButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    return doneButton;
}

Notice this line:

[doneButton addTarget:_thisController action:@selector(doneButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

The "doneButtonTapped:" method is only used in each sub class of my base class and never in the base class itself.

To avoid a warning in the base class I have to at least define the method:

- (void)doneButtonTapped:(id)sender {
}

Question:

Is there a way to avoid declaring the empty method and not get warnings?

Are blank methods like above a problem?

I know I could addTarget for the selector in each of the sub classes but there are quite a few of them. I just thought leaving everything in the doneButtonTapped method would be easier.

Was it helpful?

Solution

There is a way ;) :

NSString *stringMethod = @"doneButtonTapped:";
[doneButton addTarget:self action:NSSelectorFromString(stringMethod) forControlEvents:UIControlEventTouchUpInside];

In this way you can also change your method at runtime before create the button, just changing the string. And the warning so, are not shown in pre-compile time.

So, enjoy! ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top