Ray Y
Ray Y

Reputation: 1321

Unrecognized Selector sent to Instance in another class

I've been trying to figure out something that might be obvious and I'm missing the point or losing it. Is something wrong with my toggleStormButtons function that XCode isn't seeing it?

In my main class I have the following to call a function in another class:

STopLeftMenu *mTopLeft = [[STopLeftMenu alloc]init];    
[mTopLeft drawStormToggleButton];

Then in the other class I have 2 functions:

- (void)toggleStormButtons{
    [UIButton animateWithDuration:0.50 animations:^{
        if (stormToggleBtn.transform.tx == 0){
            [stormToggleBtn setTransform:CGAffineTransformMakeTranslation(307, 0)];
            UIImage* hideButtonImg = [UIImage imageNamed:@"aiga_right_arrow_mod_hide_resize.png"];
            [stormToggleBtn setBackgroundImage:hideButtonImg forState:UIControlStateNormal];
        }
        else{
            [stormToggleBtn setTransform:CGAffineTransformMakeTranslation(0, 0)];
            UIImage* showButtonImg = [UIImage imageNamed:@"aiga_right_arrow_mod_show_resize.png"];
            [stormToggleBtn setBackgroundImage:showButtonImg forState:UIControlStateNormal];
        }
    }];

    for(UIView* storm in stormButtonSaves){
        [UIView animateWithDuration:0.50 animations:^{
            if (storm.transform.tx == 0){
                [storm setTransform:CGAffineTransformMakeTranslation(307, 0)];
                storm.alpha = .65;
            }
            else{
                [storm setTransform:CGAffineTransformMakeTranslation(0, 0)];
                storm.alpha = 0;
            }
        }];
    }
}

- (void)drawStormToggleButton{
    //Storm Pullout Toggle Button
    UIImage *buttonImageNormal = [UIImage imageNamed:@"aiga_right_arrow_mod_show_resize.png"];
    stormToggleBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 65, 75) ];
    [stormToggleBtn setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
    stormToggleBtn.backgroundColor = [UIColor clearColor];
    stormToggleBtn.alpha = 0.5;
    [stormToggleBtn addTarget:self action:@selector(toggleStormButtons) forControlEvents:UIControlEventTouchUpInside];
    [viewsToRemove addObject:stormToggleBtn];
    [mv addSubview:stormToggleBtn];
}

I seem to be getting an unrecognized selector message:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSMallocBlock__ toggleStormButtons]: unrecognized selector sent to instance 0x7bc9ca0'
*** First throw call stack:
(0x1b9e012 0x1966e7e 0x1c294bd 0x1b8dbbc 0x1b8d94e 0x197a705 0x8ae2c0 0x8ae258 0x96f021 0x96f57f 0x96e6e8 0x8ddcef 0x8ddf02 0x8bbd4a 0x8ad698 0x2599df9 0x2599ad0 0x1b13bf5 0x1b13962 0x1b44bb6 0x1b43f44 0x1b43e1b 0x25987e3 0x2598668 0x8aaffc 0x2285 0x2185)
libc++abi.dylib: terminate called throwing an exception

Upvotes: 0

Views: 203

Answers (2)

Chuck
Chuck

Reputation: 237110

It sounds like your STopLeftMenu is being deallocated too early. The button does not retain its target, so you'll need to keep this object around as long as it needs to respond to the button's messages. If you're not sure how the object is getting deallocated, try debugging with Instruments.

Upvotes: 1

user189804
user189804

Reputation:

I don't see anything wrong with the code you have shown. I tried it in an existing app of mine and while I had to a) declare a UIButton local variable, b) change the image used for the button and c) comment out the stuff in toggleStormButtons, the method was called every time I tapped the button, no problems.

You don't show your storage for the button. Are you using ARC? Is the button strong? If ARC it should be strong. If not ARC and you are not using a property to assign with a retain, that could cause problems. What does viewsToRemove do? Looks like an array but it could be something else.

Why don't you use + buttonWithType: and set the frame later?

Upvotes: 1

Related Questions