Home > OS >  Find if a uibutton exists as a subview in an uibutton
Find if a uibutton exists as a subview in an uibutton

Time:12-24

I have a view. Inside that view i have a uibutton. Inside the uibutton i add another smaller uibutton with the following code.

self.badgeIndicatorView = [[UIButton alloc] initWithFrame:CGRectMake(self.friendsButton.frame.size.width-15, 5, 10, 10)];
self.badgeIndicatorView.backgroundColor = [UIColor redColor];
BOOL doesContain = [self.friendsButton.subviews containsObject:self.badgeIndicatorView];
 
if(!doesContain){
   [self.friendsButton addSubview:self.badgeIndicatorView];
}

doesContain always is NO. containsObject does not seem to work. I want to check if the uibutton called badgeIndicatorView already exists in the friendsButton. What am i missing? Any help appreciated.

CodePudding user response:

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!
}

CodePudding user response:

You're assigning a new object to your badgeIndicatorView property. The existing button cannot have it as a subview before adding it as a subview. You can use tag to identify a specific subview in the hierarchy of a view.

I'm assuming that you're trying to set the subview only once. For that, you can first check if a view with a specific tag exists in the button. If not, you can create the button, assign it the specific tag and add it as a subview. The next time, viewWithTag will give you the previously created object which is already added as a subview.

self.badgeIndicatorView = (UIButton *)[self.friendsButton viewWithTag:1];

if (!self.badgeIndicatorView) {
    self.badgeIndicatorView = [[UIButton alloc] initWithFrame:CGRectMake(self.friendsButton.frame.size.width-15, 5, 10, 10)];
    self.badgeIndicatorView.backgroundColor = [UIColor redColor];
    self.badgeIndicatorView.tag = 1;

    [self.friendsButton addSubview:self.badgeIndicatorView];
} 
  • Related