With this code:
// create a NEW button
self.badgeIndicatorView = [[UIButton alloc] initWithFrame:CGRectMake(self.friendsButton.frame.size.width-15, 5, 10, 10)];
self.badgeIndicatorView.backgroundColor = [UIColor redColor];
// friendsButton CANNOT contain the button you just created
BOOL doesContain = [self.friendsButton.subviews containsObject:self.badgeIndicatorView];
You would want to check if friendsButton already has a subview button.
As a side note, a better approach would be to create a subclass that adds the badgeIndicatorView button on init and sets it to hidden. Then, show or hide it as needed.
Edit
This line:
self.badgeIndicatorView = [[UIButton alloc] initWithFrame:CGRectMake(20, 10, 100, 40)];
creates a NEW button and ASSIGNS IT to self.badgeIndicatorView.
If a button has already been created and assigned to self.badgeIndicatorView, the NEW button will not be equal to the old button. The old button will still exist, but will no longer be assigned to self.badgeIndicatorView.
Easy way to see it... run this code:
self.badgeIndicatorView = [[UIButton alloc] initWithFrame:CGRectMake(20, 10, 100, 40)];
// log description of self.badgeIndicatorView
NSLog(@"1: %@", self.badgeIndicatorView.debugDescription);
self.badgeIndicatorView = [[UIButton alloc] initWithFrame:CGRectMake(20, 10, 100, 40)];
// log description of self.badgeIndicatorView
NSLog(@"2: %@", self.badgeIndicatorView.debugDescription);
self.badgeIndicatorView = [[UIButton alloc] initWithFrame:CGRectMake(20, 10, 100, 40)];
// log description of self.badgeIndicatorView
NSLog(@"3: %@", self.badgeIndicatorView.debugDescription);
and the debug output will look similar to this - notice that the object addresses are different (meaning you created 3 buttons):
1: <UIButton: 0x7f997310e310; frame = (20 10; 100 40); opaque = NO; layer = <CALayer: 0x600002208000>>
2: <UIButton: 0x7f9951f09be0; frame = (20 10; 100 40); opaque = NO; layer = <CALayer: 0x600002275e60>>
3: <UIButton: 0x7f99730065a0; frame = (20 10; 100 40); opaque = NO; layer = <CALayer: 0x600002262760>>
So, the NEW button instance, assigned to self.badgeIndicatorView, will NOT be the same button instance that was already created and added to self.friendsButton.
You can simplify things by checking if self.badgeIndicatorView is not nil ... which would mean it was already created and added:
if (!self.badgeIndicatorView) {
// create badgeIndicatorView and add it to self.friendsButton
} else {
// badgeIndicatorView already exists!
}