繁体   English   中英

简单的bool切换无法正常工作?

[英]Simple bool switching not working?

我正在翻转像这样的简单BOOL的值:

active = !active;
NSLog(@"%@", active? @"active" : @"not active");

我已经做了很多次,但是现在,由于某种原因,它不起作用,这真的让我感到沮丧。 当流动到达这里时,它总是打印“活动”。 它没有切换!!

这是我的界面:

@interface HeaderView : UIView {
    UILabel *label;
    UIButton *button;
    UIImageView *background;
    BOOL active;
    int section;

    __weak id<HeaderViewDelegate> delegate;
}

和执行操作的功能:

- (void)actionTUI:(id)sender {
    active = !active;
    NSLog(@"%@", active? @"active" : @"not active");
    UIImage *image = nil;
    if (active) {
        image = [UIImage imageNamed:@"active.png"];
    } else {
        image = [UIImage imageNamed:@"unactive.png"];
    }
    [button setBackgroundImage:image forState:UIControlStateNormal];
    [delegate headerView:self toggled:active];
}

谢谢

与所有其他类型一样, BOOL ean值在运行时初始化为NULL(0,FALSE,NO),除非分配了不同的值。 您正在为BOOL分配一个not(NO),它在每次运行时都会输出YES。

此外,您的NSLog()很奇怪,它可能以错误的方式评估自己,并且很可能导致问题。 它不应该包括文字:

NSLog(active? @"active" : @"not active");

好吧,问题是在切换之后,我正在重新加载tableview,它显然回忆起:

- (UIView *)tableView:(UITableView *)_tableView viewForHeaderInSection:(NSInteger)section {

这导致新的标题视图被分配/初始化,活动设置为NO,并且在切换后立即设置为YES。 这就是为什么每次,即使在翻转它被设置为活动的值之后(因为之后,它将被丢弃并且新的标题视图将会出现在它的位置)。

暂无
暂无

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

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