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

时间:2012-06-03 04:53:39

标签: objective-c ios cocoa-touch

我正在翻转像这样的简单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];
}

由于

2 个答案:

答案 0 :(得分:2)

与所有其他类型一样,

BOOL ean值在运行时被初始化为NULL(0,FALSE,NO),除非分配了不同的值。您正在为BOOL分配一个not(NO),它在每次运行时都会打印YES。

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

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

答案 1 :(得分:0)

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

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

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

相关问题