开发者

UIButton not setting alpha properly

开发者 https://www.devze.com 2023-04-05 18:56 出处:网络
This is probably a stupid mistake of mine, but I have 3 UIbuttons, 2 of which are hidden by default, and 1 which is supposed to reveal the 2 others when clicked on. I call this method in my viewDidLoa

This is probably a stupid mistake of mine, but I have 3 UIbuttons, 2 of which are hidden by default, and 1 which is supposed to reveal the 2 others when clicked on. I call this method in my viewDidLoad:

-(void) initButtons
{
self.checkbutton = [[UIButton alloc] initWithFrame:CGRectMake(20, 330, 80, 70)];
self.checkbutton.backgroundColor = [UIColor clearColor];
[self.checkbutton setBackgroundImage:[UIImage imageNamed:@"checkbutton"] forState:UIControlStateNormal];
self.checkbutton.enabled = NO;
self.checkbutton.alpha = 0.0;
[self.view addSubview:self.checkbutton];

self.xbutton = [[UIButton alloc] initWithFrame:CGRectMake(230, 330, 70, 70)];
self.xbutton.backgroundColor = [UIColor clearColor];
[self.xbutton setBackgroundImage:[UIImage imageNamed:@"X.png"] forState:UIControlStateNormal];
self.xbutton.enabled = NO;
self.xbutton.alpha = 0.0;
[self.view addSubview:self.xbutton];
}

And when my visible button is clicked, this method is called:

-(void) showbuttons
{
    self.checkbutton.enabled = YES;
    [[self checkbutton] setAlpha:1.0];

    self.xbutton.enabled = YES;
    [[self xbutton] setAlpha:1.0];

    NSLog(@"xbutton alpha: %f", [[self xbutton] alpha]);
    NSLog(@"checkbutton alpha: %f", [[self checkbutton] alpha]);
}

What happens is that when I click the visible button, only 1 button appears. (xbutton) They should be coded开发者_如何学JAVA in the exact same way, I don't know what's wrong. The NSLogs that I have show that both buttons have an alpha value of 1.0. I'm sure I @propertied and @synthesized checkbutton correctly, it's just like the xbutton.

What am I doing wrong?

Also, a side question: what's the difference between [checkbutton setAlpha] and [self.checkbutton setAlpha]?

Ughh... My fault. My image was named checkmark.png and not checkbutton.png. Thanks to @DanielRHicks for answering my side question though.


hidden and alpha are different

3 UIbuttons, 2 of which are hidden by default, and 1 which is supposed to reveal the 2 others when clicked on.

you say that they are hidden by default

maybe you should try

self.checkbutton.hidden = NO


are you setting the hidden property initially to YES? if so you will also have to do

self.xbutton.hidden = NO

on the buttons

0

精彩评论

暂无评论...
验证码 换一张
取 消