NSSegmentedControl -selectedSegment 始终返回 0

发布于 2024-09-01 17:54:51 字数 1150 浏览 8 评论 0原文

我有一个 NSSegmentedControl,其中两个段在界面生成器中设置为“无选择”模式。无论我尝试什么,我都无法让 -selectedSegment 返回除 0 之外的任何内容,即使默认情况下段 0 甚至被禁用并且不可能被选择。这是当您单击控件上的任何段时调用的相关函数:

-(IBAction)changeStep:(id)sender
{
    [stepContainer setHidden:TRUE];
    [(NSView *)[[wizard stepArray] objectAtIndex:(NSInteger)[wizard step]] removeFromSuperview];
    switch ([[navigationButton cell] selectedSegment])
    {
    case 0:
        [wizard setStep:(NSInteger *)[wizard step]-1];
        break;
    case 1:
        [wizard setStep:(NSInteger *)[wizard step]+1];
        break;
    default:
        break;
    }
    //[[navigationButton cell] setSelected:FALSE forSegment:[navigationButton selectedSegment]];

    if ([wizard step] > 0)
    {
        [wizard setStep:0];
        [navigationButton setEnabled:FALSE forSegment:0];
    }

    NSLog(@"%d", [wizard step]);
    [stepContainer addSubview:(NSView *)[[wizard stepArray] objectAtIndex:(NSInteger)[wizard step]]];
    [stepContainer setHidden:FALSE withFade:TRUE];
}

我也尝试过使用 -isSelectedForSegment,但它具有相同的结果。
你能提供的任何帮助都会很棒,我不知道我做错了什么。谢谢!
SphereCat1

编辑:这是一个捂脸时刻,请参阅下面我的答案。

I have an NSSegmentedControl with two segments set to "Select None" mode in Interface Builder. No matter what I try, I can't get -selectedSegment to return anything but 0, even though segment 0 is even disabled by default and can't possibly be selected. Here's the relevant function that gets called when you click any segment on the control:

-(IBAction)changeStep:(id)sender
{
    [stepContainer setHidden:TRUE];
    [(NSView *)[[wizard stepArray] objectAtIndex:(NSInteger)[wizard step]] removeFromSuperview];
    switch ([[navigationButton cell] selectedSegment])
    {
    case 0:
        [wizard setStep:(NSInteger *)[wizard step]-1];
        break;
    case 1:
        [wizard setStep:(NSInteger *)[wizard step]+1];
        break;
    default:
        break;
    }
    //[[navigationButton cell] setSelected:FALSE forSegment:[navigationButton selectedSegment]];

    if ([wizard step] > 0)
    {
        [wizard setStep:0];
        [navigationButton setEnabled:FALSE forSegment:0];
    }

    NSLog(@"%d", [wizard step]);
    [stepContainer addSubview:(NSView *)[[wizard stepArray] objectAtIndex:(NSInteger)[wizard step]]];
    [stepContainer setHidden:FALSE withFade:TRUE];
}

I've also tried using -isSelectedForSegment, but it has the same result.
Any help you can provide would be awesome, I have no idea what I'm doing wrong. Thanks!
SphereCat1

EDIT: This is a facepalm moment, see my answer below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浴红衣 2024-09-08 17:54:52

好吧,这是一个捂脸的时刻。事实证明,我的 NSLogging 是错误的,并且我的步骤递增/递减逻辑中存在错误。

一切都好起来了! :)

捂脸
球体猫1

Alright, this is a facepalm moment. Turns out I was NSLogging the wrong thing, and there's just an error in my step increment/decrement logic.

All better! :)

facepalm
SphereCat1

无所谓啦 2024-09-08 17:54:52

您似乎在不需要的地方做了很多转换。

[(NSView *)[[wizard stepArray] objectAtIndex:(NSInteger)[wizard step]] removeFromSuperview];

[向导步骤] 返回什么?您不需要投射它。

    [wizard setStep:(NSInteger *)[wizard step]-1];

这更加不一致。 NSInteger * 是指针类型(NSInteger 不是对象)。如果 [wizard setStep:] 采用 NSInteger *,那么可能是因为它被用作 inout 参数,也许是为了向您提供新步骤(如果它不同)从要求的一个。在这种情况下,您需要执行以下操作:

NSInteger newStep = [wizard step] - 1;
[wizard setStep: &newStep];

You seem to be doing a lot of casting in places that you shouldn't need to.

[(NSView *)[[wizard stepArray] objectAtIndex:(NSInteger)[wizard step]] removeFromSuperview];

What does [wizard step] return? You should not need to cast it.

    [wizard setStep:(NSInteger *)[wizard step]-1];

This is even more inconsistent. NSInteger * is a pointer type (NSInteger is not an object). If [wizard setStep:] takes a NSInteger *, then it is probably because it's being used as an inout parameter, perhaps to provide you with the new step if it is different from the requested one. In that case, you need to do something like this:

NSInteger newStep = [wizard step] - 1;
[wizard setStep: &newStep];
盛夏已如深秋| 2024-09-08 17:54:52

您确定 navigationButton 已初始化吗?将 NSLog 语句如下所示:

NSLog (@"%@", navigationButton);

如果您向 nil 对象发送消息,您将得到 0 结果。如果 navigationButtonIBOutlet,您需要确保它在 Interface Builder 中正确链接。

Are you sure that navigationButton is initialised? Put an NSLog statement like this:

NSLog (@"%@", navigationButton);

If you message a nil object, you will get 0 as a result. If navigationButton is an IBOutlet you need to make sure it is properly linked up in Interface Builder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文