6 Replies Latest reply: Apr 30, 2009 8:09 AM by kai68
typewriter Level 1 Level 1 (30 points)
Hello- I'm subclassing UIButton, to add a property for the button's index. However when I try to set that property, it throws an unrecognized selector:


reason: '* -[UIRoundedRectButton setIndex:]: unrecognized selector sent to instance 0x45e590'


I set up the button here-


IndexButton *b1 = [[IndexButton buttonWithType:UIButtonTypeRoundedRect] initWithFrame:CGRectMake(x,y,w,h)];


then:


[b1 setIndex:i]; //throws an unrecognized selector


IndexButton, the subclass of UIButton is set up properly, with index synthesized, etc. I've done this exact thing with other UIControls. Its treating the UIButtonTypeRoundedRect as if it were a separate class, but it is just an enum property in UIbutton.

Skynet MBP, Mac OS X (10.5.4)
  • 1. Re: [iPhone] UIButton subclass strangeness
    lawicko Level 1 Level 1 (25 points)
    I had the same problem some time ago, but unfortunately I wasn't able to find any solution or workaround. I had to give up using UIButtons :-/
    There is some kind of connection between button type and button instance, note that buttons inialized through alloc init are not visible at all. So the only way to create usable button is to use the covenience method (because after creating an instance you cannot set the type). To sum up - it doesn't make any sense to subclass UIButton
    In case I'm wrong please somebody more experienced just correct me.
  • 2. Re: [iPhone] UIButton subclass strangeness
    lawicko Level 1 Level 1 (25 points)
    I had the same problem some time ago, but unfortunately I wasn't able to find any solution or workaround. I had to give up using UIButtons :-/
    There is some kind of connection between button type and button instance, note that buttons inialized through alloc init are not visible at all. So the only way to create usable button is to use the covenience method (because after creating an instance you cannot set the type). To sum up - it doesn't make any sense to subclass UIButton
    In case I'm wrong please somebody more experienced just correct me.

    PS Sorry for double post, didn't know I had to wait a while to se my reply.

    Message was edited by: lawicko
  • 3. Re: [iPhone] UIButton subclass strangeness
    typewriter Level 1 Level 1 (30 points)
    Well that is dissapointing.
  • 4. Re: [iPhone] UIButton subclass strangeness
    David Dennis Level 1 Level 1 (45 points)
    It's a little confusing but I figured it out. My class is called ButtonPoint and here is my initWithFrame:image:

    + (id)initWithFrame:(CGRect)frame image: (NSString *) image {
    ButtonPoint *b;

    b = [ ButtonPoint buttonWithType: UIButtonTypeCustom ];

    if (!b) {
    NSLog(@"Could not create button");
    return(self);
    }

    b.frame = frame;

    // Initialization code
    b.centerpoint = CGPointMake(b.center.x, b.center.y);

    [ b setImage: [ UIImage imageNamed: image ] forState: UIControlStateNormal ];

    return b;
    }

    Works like a charm.

    Hope that helps.

    D
  • 5. Re: [iPhone] UIButton subclass strangeness
    mobhaku Level 1 Level 1 (0 points)
    David,

    Do you also have added new methods to your subclassed button?
    And if so, do they work on target?
    Currently in my situation subclassing works on the emulator, but not on target, there I get the unrecognized selector crash.
    Can you please provide more information on how you have subclassed the UIButton and how you are using it?
    thanx and best regards,

    Harreld.
  • 6. Re: [iPhone] UIButton subclass strangeness
    kai68 Level 1 Level 1 (0 points)
    Hi,
    I had the same proble: Initialization of the subclassed button was possible, but invocation of the methods of the subclass wasn't.
    I did a workround as follows:
    subclassed UIView, added the methods there and whenever using the button I put it on the subclassed UIView. Then when the method invocation is needed I call
    [[[myButton superview] myMethod]];
    instead of
    [[myButton myMethod]];

    Message was edited by: kai68