NSSegmentedControl -selectedSegment 始终返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,这是一个捂脸的时刻。事实证明,我的 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
您似乎在不需要的地方做了很多转换。
[向导步骤]
返回什么?您不需要投射它。这更加不一致。
NSInteger *
是指针类型(NSInteger
不是对象)。如果[wizard setStep:]
采用NSInteger *
,那么可能是因为它被用作 inout 参数,也许是为了向您提供新步骤(如果它不同)从要求的一个。在这种情况下,您需要执行以下操作:You seem to be doing a lot of casting in places that you shouldn't need to.
What does
[wizard step]
return? You should not need to cast it.This is even more inconsistent.
NSInteger *
is a pointer type (NSInteger
is not an object). If[wizard setStep:]
takes aNSInteger *
, 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:您确定
navigationButton
已初始化吗?将NSLog
语句如下所示:如果您向
nil
对象发送消息,您将得到0
结果。如果navigationButton
是IBOutlet
,您需要确保它在 Interface Builder 中正确链接。Are you sure that
navigationButton
is initialised? Put anNSLog
statement like this:If you message a
nil
object, you will get0
as a result. IfnavigationButton
is anIBOutlet
you need to make sure it is properly linked up in Interface Builder.