-2

I'm making a simple UIButton subclass.

.h file:

@interface VUFollowButton : UIButton

/**
 Designated initializer

 @param follow Highlights the button if true
 */
+ (instancetype)buttonWithFollow:(BOOL)follow;

/*
 * Setting this bool to YES will highlight the button and change the text to "following"
 * Default is "follow"
 */
@property (nonatomic, assign) BOOL following;


@end

.m file:

 #import "VUFollowButton.h"

@implementation VUFollowButton

+ (instancetype)buttonWithFollow:(BOOL)follow {

    VUFollowButton* followButton = (VUFollowButton*)[UIButton buttonWithType:UIButtonTypeCustom];
    [followButton setTitleEdgeInsets:UIEdgeInsetsMake(2., 8., 2., 8.)];
    followButton.layer.borderColor = [UIColor whitColor];
    followButton.layer.borderWidth = 2.0f;
    [followButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    followButton.titleLabel.font = [UIFont nationalBoldWithSize:17];
    followButton.following = follow;

    return followButton;
}

- (void)setFollowing:(BOOL)following {

    if (!following) {
        self.backgroundColor = [UIColor clearColor];
        [self setTitle:@"FOLLOW" forState:UIControlStateNormal];
    }
    else {
        self.backgroundColor = [UIColor blackColor];
        [self setTitle:@"FOLLOWING" forState:UIControlStateNormal];
    }
}

@end

But on the line followButton.following = follow;, I'm getting:

-[UIButton setFollowing:]: unrecognized selector sent to instance 0x7f9c10f809b0

If I set a breakpoint before that line, followButton shows as a VUFollowButton in the variable debugger and has a property called following.

I know I'm missing something basic here.

OdieO
  • 6,836
  • 7
  • 56
  • 88
  • Where's the code that actually adds `setFollowing:` as a target for the button? – rmaddy Apr 28 '15 at 19:23
  • possible duplicate of [How can I debug 'unrecognized selector sent to instance' error](http://stackoverflow.com/questions/25853947/how-can-i-debug-unrecognized-selector-sent-to-instance-error) – Hot Licks Apr 29 '15 at 00:22
  • To access the methods of VUFollowButton you have to actually have a VUFollowButton, not a UIButton. – Hot Licks Apr 29 '15 at 00:24

2 Answers2

3

You're instantiating a UIButton and casting it to VUFollowButton. The instance that you're returning is of type UIButton, which doesn't have a setFollowing: accessor method or following property. The reason the following property is visible is because you've casted it to the type VUFollowButton.

E.g.

VUFollowButton* followButton = (VUFollowButton*)[UIButton buttonWithType:UIButtonTypeCustom];

Should be:

VUFollowButton* followButton = [VUFollowButton buttonWithType:UIButtonTypeCustom];
Jonathan
  • 2,623
  • 3
  • 23
  • 38
  • Probably worth noting that the `UIButton` documentation says to use alloc/init directly if you want to create an instance of a `UIButton` subclass and not to use `buttonWithType:` – dan Apr 28 '15 at 19:59
0

At the following line

VUFollowButton* followButton = (VUFollowButton*)[UIButton buttonWithType:UIButtonTypeCustom];

you actually create UIButton. Change it to the

VUFollowButton* followButton = [self buttonWithType:UIButtonTypeCustom];
Azat
  • 6,745
  • 5
  • 31
  • 48