Objective-C A selector call that the compiler does not consider exists (although we know that it is)

I have this code in prepareForSegue method

    // Get destination view
    UIViewController *viewController = [segue destinationViewController];

    //See if it responds to a selector
    if ([viewController respondsToSelector:@selector(setSomethingOrOther:)]) {
        //if so call it with some data
        [viewController setSomethingOrOther:something];
    }

The code above means that I don’t need to include a link to the actual controller class of the view to which it is attached. I can more freely bind the two view controllers and just check if it responds to any property set on it.

The problem is that when I do this, I get the following compile time error:

No visible @interface for 'UIViewController' declares selector 'setSomethingOrOther:'

which is true of course. I know I can get around this by including a link to the view, but I would prefer to leave it separated. How can i get around this

+5
3

performSelector:aSelector, .

+10
[viewController performSelector:@selector(setSomethingOrOther:) 
                     withObject:something];
+7

You can also do it

[(id)viewController setSomethingOrOther:something];

in some situation, but I will complain if the compiler does not know about the existence of setSomethingOrOther: in general, as a library in which you did not include the header.

+3
source

All Articles