Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress "undeclared selector" warning in Xcode 5

I have a parent view controller and a child view controller. In child view controller's view, I have a UIButton called "startButton". To that button, I have added a target as below

[startButton addTarget:[self parentViewController] action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];

I have implemented, test: method in parent controller and this works perfectly fine for me. When the button is tapped, the event is passed from child view controller to parent view controller and code snippets within test: method gets executed.

But my problem is, I am getting a warning message, "Undeclared selector test:". I know, I am getting it because test: is not implemented in child view controller implementation file.

Is there any way to suppress this warning alone? Most of the suggestions I have seen here makes the entire warnings to get suppressed but I want to suppress above mentioned warning alone.

Thanks

like image 376
slysid Avatar asked Feb 06 '14 08:02

slysid


3 Answers

There is no need to suppress the warning if you can avoid it:

Declare the test: method in the @interface (in the .h file) of the parent view controller, and import that .h file in the child view controller's implementation (.m) file.

Then the compiler knows about the method and you don't get a warning about an undeclared selector anymore.

like image 117
Martin R Avatar answered Nov 16 '22 19:11

Martin R


If you know for sure that you've done the right thing, and the code actually works fine, you can stop Xcode warning you about it by surrounding the method with #pragma clang preprocessor commands.

For example:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"

[startButton addTarget:[self parentViewController] action:@selector(test:) forControlEvents:UIControlEventTouchUpInside]; 

#pragma clang diagnostic pop

Any 'missing messages' between the push and pop commands will not be reported by Xcode. I suggest having as little code as possible between them to prevent real problems in your code being unreported.

like image 24
Ben Clayton Avatar answered Nov 16 '22 20:11

Ben Clayton


Following code will make this warning as disappear( dynamic create selector). But it will crash in runtime if undeclared in parentviewcontroller.

[startButton addTarget:[self parentViewController] action:NSSelectorFromString(@"test:") forControlEvents:UIControlEventTouchUpInside];
like image 1
Mani Avatar answered Nov 16 '22 21:11

Mani