Search code examples
iosobjective-cexc-bad-accessobjective-c-category

Selector from button created in category calls released method


I have a category of a class that creates a button that initializes properly with the following options

[cancelButton addTarget:self action:@selector(cancelReconnect:) forControlEvents:UIControlEventTouchUpInside];

then in the same category.m file I have the method

-(void)cancelReconnect{ NSLog(@"here!"); }

When the button is pressed in the viewcontroller that imports the class which imports the category I get an EXC_BAD_ACCESS and it appears my method/class seems like it was released.

If I put the same cancelReconnect method in the viewcontroller where 'self' would be. It is the same result.

Is my addTarget correct? is my selector method being released? How to solve this?

note the toolBarItems array in my custom class' .h file is strong but I don't know if the button in the array is. Does the View Controller keep a strong pointer to the Class's category method


Solution

  • You are making a common mistake. Your actual method is named cancelReconnect but you tell the button that the selector is named cancelReconnect: (notice the colon).

    Change your method to:

    - (void)cancelReconnect:(UIButton *)button {
        NSLog(@"here!");
    }