1

我有NSRadioModeMatrix3个细胞。- (IBAction) EnableProxy:(id)inSender连接到NSRadioModeMatrix

- (IBAction) EnableProxy:(id)sender
{   

    if ([[sender selectedCell] tag]==2)
    { 

/*(gdb) p (int)[[inSender selectedCell] tag]
$1 = 2*/
        if (condition) {
             // select cell with tag 0
            [sender selectCellAtRow:0 column:0]; // This is not working
        }
        /*(gdb) p (int)[[inSender selectedCell] tag]
$2 = 1*/
    }
}// selection is not visible

当条件为真时。选择将返回到标签 2(旧的选定单元格)。示例项目

4

2 回答 2

4

老实说,这是预期的行为,并且有充分的理由。

在此处输入图像描述

鉴于上述 UI,如果用户点击C,他们希望C被选中。相反,如果用户单击C并被A选中,则您的用户会感到困惑(并且可能会感到沮丧)。实际上,实现这种行为违反了OS X Human Interface Guidelines for Radio Buttons

也就是说,我将继续讨论如何实现所需的行为。基本上,当您 select 时C,立即调用 selectA似乎没有成功的原因是,从技术上讲,C它仍在被选中的过程中。换句话说,事件的简化时间线如下所示:

  1. 开始点击矩阵
  2. EnableProxy:方法调用
  3. 矩阵立即被告知选择A
  4. 完成点击矩阵,C最终完全被选中

为了获得您想要的结果A,您需要像以下代码一样将请求“排队”,而不是立即告诉矩阵选择:

- (IBAction) EnableProxy:(id)inSender {
    if ([[inSender selectedCell] tag] == 2) {
//        [mProxyEnable selectCellAtRow:0 column:0];
        [self performSelector:@selector(selectFirstMatrixRow) withObject:nil
                                                              afterDelay:0.0];
    }
}

- (void)selectFirstMatrixRow {
    NSLog(@"[%@ %@]", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
    [mProxyEnable selectCellAtRow:0 column:0];
}

通过使用[self performSelector:withObject:afterDelay:],您基本上说“尽快调用此方法”。这样做可以C在调用该方法之前完全选择,这允许您想要的结果。换句话说,事件的时间线应该是这样的:

  1. 开始点击矩阵
  2. EnableProxy:方法调用
  3. 完成点击矩阵,C最终完全被选中
  4. --------- 控制返回正常的事件循环 ---------
  5. selectFirstMatrixRow方法调用
  6. 矩阵选择A
于 2013-01-29T17:53:49.050 回答
0

参考 NSGod 提供的上述答案,这也可以使用 GCD 来实现,而不是用 0.0 给出不必要的延迟

ViewController * __weak weakRef = self;
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakRef selectFirstMatrixRow];
        });
于 2015-01-16T14:31:08.610 回答