0

我正在翻转一个简单的 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];
}

谢谢

4

2 回答 2

2

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

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

NSLog(active? @"active" : @"not active");
于 2012-06-03T05:14:11.913 回答
0

好的,问题是切换后,我正在重新加载显然回忆起的表格视图:

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

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

于 2012-06-03T05:23:48.810 回答