简体   繁体   English

单击按钮时视图未更新

[英]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. 您只是忘了在两个IBActions方法中都调用方法example1。 YOu just write down [self example1] into both of the IBActions method. 您只需将[self example1]记入两个IBActions方法中。 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");
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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