我有一对 UISwitches 通过 valueChanged 事件连接到 IBAction。当触摸开关时, valueChanged 事件会正常触发。但是,如果我以编程方式更改其中一个开关,它不会调用我的 IBAction。
- (IBAction)switchChanged:(UISwitch *)sender {
if (sender == self.shippingSwitch) {
if (self.shippingSwitch.on && !self.PayPalSwitch.on) {
[self.PayPalSwitch setOn:YES animated:YES];
}
}
if (sender == self.PayPalSwitch) {
if (!self.PayPalSwitch.on) {
// This is not working when the PayPal switch is set via the code above
self.PayPalEmailField.backgroundColor = [UIColor grayColor];
self.PayPalEmailField.enabled = NO;
if (self.shippingSwitch.on) {
[self.shippingSwitch setOn:NO animated:YES];
}
} else {
self.PayPalEmailField.backgroundColor = [UIColor clearColor];
self.PayPalEmailField.enabled = YES;
}
}
}
这是正确且理想的行为。由于您显式更改了该值,因此由您决定如何处理更改后的值。
这样做的原因是,在通过用户交互通知其值已更改后,显式更改控件的值并不罕见。如果显式状态更改导致事件再次触发,您将陷入无限循环。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)