iPhone UISwitch 与另一个 UISwitch 发生反应

发布于 2024-10-31 15:03:55 字数 534 浏览 6 评论 0原文

我已经阅读了有关 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 技术交流群。

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

发布评论

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

评论(2

梦归所梦 2024-11-07 15:03:55

注册两个 UISwitch 以观察另一个开关的 UIControlEventValueChanged 通知。类似于:

- (void)viewDidLoad { // or whatever method is appropriate
    UISwitch *a = <# initialize a #>;
    UISwitch *b = <# initialize b #>;

    [a addTarget:self action:@selector(toggleB:)   
         forControlEvents:UIControlEventValueChanged]; 

    [b addTarget:self action:@selector(toggleA:)   
         forControlEvents:UIControlEventValueChanged]; 
}

- (IBAction)toggleA:(id)sender {
    [b setOn:NO animated:YES];
}

- (IBAction)toggleB:(id)sender {
    [a setOn:NO animated:YES];
}

您还可以在 Interface Builder 中设置这种关系。

Register both UISwitches to observe UIControlEventValueChanged notifications against the the other switch. Something like:

- (void)viewDidLoad { // or whatever method is appropriate
    UISwitch *a = <# initialize a #>;
    UISwitch *b = <# initialize b #>;

    [a addTarget:self action:@selector(toggleB:)   
         forControlEvents:UIControlEventValueChanged]; 

    [b addTarget:self action:@selector(toggleA:)   
         forControlEvents:UIControlEventValueChanged]; 
}

- (IBAction)toggleA:(id)sender {
    [b setOn:NO animated:YES];
}

- (IBAction)toggleB:(id)sender {
    [a setOn:NO animated:YES];
}

You can also set this relationship up in Interface Builder.

假装不在乎 2024-11-07 15:03:55

请注意您如何将两个开关设置为相同的值:

[newsPre setOn:setting animated:YES];
[techPre setOn:setting animated:YES];

您想要检查哪一个是发送者,然后将另一个设置为与设置相反的值。为此,您需要在每个开关上设置一个标签,或者可能对其进行子类化。有关检测哪个交换机是哪个交换机的更多信息,请参阅 这篇文章,其中有一些很好的信息。

Notice how you are setting both switches to the same value here:

[newsPre setOn:setting animated:YES];
[techPre setOn:setting animated:YES];

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.

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