Yarneo
Yarneo

Reputation: 3000

Cannot hide programmatically created UIButton

This issue is baffling me for a few days now.

I have 2 UIButtons, one created using the Storyboard and one created programmatically.

When I press one of the buttons, I am moved to a method that hides both the programatically built and storyboard buttons, but only the storyboard button disappears, whereas the programmatically built button remains. I have tried not only hiding but also changing the frame of the programmatically built button, but to no avail. Here is the code used:

@property (strong, nonatomic) UIButton *catBut;
@property (weak, nonatomic) IBOutlet UIButton *whenButton;
....
....
-(void)viewDidLoad {
....
    [self customizeButton:self.catBut withFrame:CGRectMake(165, 113, 135, 50)
          andAction:@selector(selectedCat) andColor:[UIColor belizeHoleColor] 
          andTag:2 andImage:@"coffee-64.png" andText:@"Cafes" andAlignmentLeft:YES];
....
}

- (void)customizeButton:(UIButton *)but withFrame:(CGRect)rect andAction:(SEL)action
                        andColor:(UIColor *)col andTag:(NSUInteger)tag 
                        andImage:(NSString *)imageName 
                        andText:(NSString *)buttonText 
                        andAlignmentLeft:(BOOL)alignLeft {
    but = [UIButton buttonWithType:UIButtonTypeCustom];
    [but setFrame:rect];
    [but setTitle:buttonText forState:UIControlStateNormal];
    but.tag = tag;
    but.titleLabel.numberOfLines = 0;
    but.titleLabel.font = [UIFont boldFlatFontOfSize:18]; 
    [but setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    CGFloat spacing = 10; // the amount of spacing to appear between image and title
    but.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    but.imageEdgeInsets = UIEdgeInsetsMake(0, spacing, 0, 0);
    but.titleEdgeInsets = UIEdgeInsetsMake(0, spacing*2, 0, 0);
    [but setTitleColor:[UIColor cloudsColor] forState:UIControlStateNormal];
    [but setTitleColor:[UIColor cloudsColor] forState:UIControlStateHighlighted];
    [but addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [self.buttonsView addSubview:but];
}
....
- (void)selectedCat {
    self.catBut.hidden = YES;
    self.whenButton.hidden = YES;
}

Why is only the Storyboard button compliant to UI changes? Is there something I am missing? I have tried cancelling the AutoLayout but that didn't change a thing.

Upvotes: 0

Views: 142

Answers (2)

Mani
Mani

Reputation: 17585

It is BOOL type, not an bool type.

try this

self.catBut.hidden = YES;
self.whenButton.hidden = YES;

I can see this property catBut, But I cann't see creation for this, as well I didn't give Outlet too. Check this. You've tried something but simply fix by following line.

[self.buttonsView addSubview:but];
self.catBut = but;

Upvotes: 0

nswamy
nswamy

Reputation: 1051

The issue is with the passing for argument in your method (customizeButton:(UIButton *)but).

Change your code to this and check

but = [UIButton buttonWithType:UIButtonTypeCustom];

to

   self.catBut = [UIButton buttonWithType:UIButtonTypeCustom];
......
[self.buttonsView addSubview:self.catBut];

OR You should pass the address customizeButton:(UIButton **)but in the method

and while calling use &self.catBut

Upvotes: 3

Related Questions