A UIView subclass throws a “Selector Not Found” when calling the UIView + Category method

I have a subclass of UIView, MyView. I also have a category in UIView called UIView + simpleCategory.

This category declares the doSomething method.

@interface UIView (simpleCategory) - (void) doSomething; @end 

I'm having problems calling the doSomething method from a subclass of UIView MyView. I get a "selector not detected" error. I was wondering what I need to do to make the subclass recognize the methods of the superclass class.

The problem occurs when calling the method of the UIView category in an instance of my subclass of UIView:

 MyView *view = [[MyView alloc] init]; [view doSomething]; // throws selector not found error here 

I am wondering if I skip #import somewhere, but I would like to understand the relationship between categories and subclasses.

DECISION::

Apparently my implementation was wonderful. I just needed to add a category to the application. I did this by clicking a category in Project Navigator. Then I clicked on the Utilities icon (the view that opens on the right side of the window) and checked the Target Membership checkbox in the File Inspector menu. That was all that had to be done. Thanks everyone for the answers.

+4
source share
2 answers

Your project has a header file declaring the UIView category, otherwise you will get a compilation warning.

But at runtime, it does not find the category method. Check your implementation file for the UIView category to see what goals it includes. I am sure that it is not included in your main goal.

+5
source

Well, here is what I did to test this, and it worked.

Category:

 @interface UIView (simpleCategory) -(void)doSomething; @end @implementation UIView (simpleCategory) -(void)doSomething { NSLog(@"Doing something"); } 

I created a subclass of UIView, MyView, to which I did not add code, except to import the category:

 #import <UIKit/UIKit.h> #import "UIView+simpleCategory.h" @interface MyView : UIView @end 

Then, in my opinion, the controller's viewDidLoad method:

 MyView *view = [[MyView alloc] init]; [view doSomething]; 
+2
source

Source: https://habr.com/ru/post/1435813/


All Articles