简体   繁体   中英

View is not updating when the button is clicked

The problem is that when the button is clicked, it is not updating! it is not hiding or showing the objects like it's written in the code. What am I missing?

viewcontroller.h

@interface ViewController : UIViewController {
    BOOL clicked1;
    BOOL clicked2;
}

@property (strong, nonatomic) IBOutlet UIImageView *buttonbg1;
@property (strong, nonatomic) IBOutlet UIImageView *buttonbg11;
@property (strong, nonatomic) IBOutlet UIImageView *buttonbg111;
@property (strong, nonatomic) IBOutlet UIButton *exaa1;
@property (strong, nonatomic) IBOutlet UIButton *exab2;

- (IBAction)exaa1:(id)sender;
- (IBAction)exab2:(id)sender;

@end

viewcontroller.m

- (IBAction)exaa1:(id)sender {
    clicked1 = YES;
}

- (IBAction)exab2:(id)sender {
    clicked2 = YES;
}

- (void)example1 {
    [_exaa1 setTitle:@"1111" forState:UIControlStateNormal];
    [_exab2 setTitle:@"2222" forState:UIControlStateNormal];

    if (clicked1) {
        _buttonbg111.hidden = NO;
        _buttonbg11.hidden = YES;
        _buttonbg1.hidden = YES;
        NSLog(@"1");

    } else if(clicked2) {
        _buttonbg11.hidden = NO;
        _buttonbg1.hidden = YES;
        _buttonbg111.hidden = YES;
        NSLog(@"2");
    }
}
*- (IBAction)exaa1:(id)sender {
    clicked1 = YES;
    [self example1];
}
- (IBAction)exab2:(id)sender {
    clicked2 = YES;
    [self example1];
}
- (void)example1 {
    [_exaa1 setTitle:@"1111" forState:UIControlStateNormal];
    [_exab2 setTitle:@"2222" forState:UIControlStateNormal];
    if (clicked1) {
        _buttonbg111.hidden = NO;
        _buttonbg11.hidden = YES;
        _buttonbg1.hidden = YES;
        NSLog(@"1");
    } else if(clicked2) {
        _buttonbg11.hidden = NO;
        _buttonbg1.hidden = YES;
        _buttonbg111.hidden = YES;
        NSLog(@"2");
    }
}*

You just forget to call your method example1 in your both IBActions Method. YOu just write down [self example1] into both of the IBActions method. And you will get your exact output.

您忘记在两个按钮IBAction方法中都调用example1方法

Please refer the following corrected code   
 - (IBAction)exaa1:(id)sender {
        clicked1 = YES;
        clicked2 = NO;
        [self example1];
    }

    - (IBAction)exab2:(id)sender {
        clicked2 = YES;
        clicked1 = NO;
        [self example1];
    }

    - (void)example1 {
        [_exaa1 setTitle:@"1111" forState:UIControlStateNormal];
        [_exab2 setTitle:@"2222" forState:UIControlStateNormal];

        if (clicked1) {
            _buttonbg111.hidden = NO;
            _buttonbg11.hidden = YES;
            _buttonbg1.hidden = YES;
            NSLog(@"1");

        } else if(clicked2) {
            _buttonbg11.hidden = NO;
            _buttonbg1.hidden = YES;
            _buttonbg111.hidden = YES;
            NSLog(@"2");
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM