iPhone UISwitch 与另一个 UISwitch 发生反应
我已经阅读了有关 UISwitches 的所有内容,但我似乎无法在我的情况下弄清楚它们。 我有 2 个 UISwitch。为了使我的代码正常工作,只能打开其中之一。我将如何实现这个目标?
到目前为止,我已经尝试过...
MyClass.h
-(IBAction)sufSwitchChanged:(id)sender;
MyClass.m
-(IBAction)preSwitchChanged:(id)sender {
UISwitch *whichSwitch = (UISwitch *)sender;
BOOL setting = whichSwitch.isOn;
[newsPre setOn:setting animated:YES];
[techPre setOn:setting animated:YES];
}
....它有效,但它使两个开关都打开或关闭。我只需要弄清楚如何防止它们同时打开。
I've read all about UISwitches already but I cannot seem to figure them out in my situation.
I have 2 UISwitches. In order for my code to work only one of them can be ON. How would I accomplish this?
So far i've tried...
MyClass.h
-(IBAction)sufSwitchChanged:(id)sender;
MyClass.m
-(IBAction)preSwitchChanged:(id)sender {
UISwitch *whichSwitch = (UISwitch *)sender;
BOOL setting = whichSwitch.isOn;
[newsPre setOn:setting animated:YES];
[techPre setOn:setting animated:YES];
}
....which worked but it made both switches ON or OFF. I just need to figure out how to prevent them from both being ON at the same time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注册两个 UISwitch 以观察另一个开关的 UIControlEventValueChanged 通知。类似于:
您还可以在 Interface Builder 中设置这种关系。
Register both UISwitches to observe UIControlEventValueChanged notifications against the the other switch. Something like:
You can also set this relationship up in Interface Builder.
请注意您如何将两个开关设置为相同的值:
您想要检查哪一个是发送者,然后将另一个设置为与
设置
相反的值。为此,您需要在每个开关上设置一个标签,或者可能对其进行子类化。有关检测哪个交换机是哪个交换机的更多信息,请参阅 这篇文章,其中有一些很好的信息。Notice how you are setting both switches to the same value here:
You want to check which one is the sender and then set the other one to the opposite of
setting
. To do so, you will need to set a tag on each switch, or perhaps subclass it. For more information on detecting which switch is which, see this post, which has some good information.