【发布时间】:2016-03-23 19:54:14
【问题描述】:
每次我在模拟器中将 UISwitch 切换为打开或关闭时,即使其中一个开关打开,它仍然返回 false。我什至尝试添加
self.switch1.on = true;
在 checkSwitch1Changed 的 if 语句中。
这是我在 .m 文件中的代码。
- (IBAction)checkSwitch1Changed:(id)sender {
if (self.switch1.on)
{
self.switch2.on = NO;
self.switch3.on = NO;
self.switch4.on = NO;
}
...
}
- (IBAction)checkSwitch2Changed:(id)sender {
if (self.switch2.on)
{
self.switch1.on = NO;
self.switch3.on = NO;
self.switch4.on = NO;
}
...
}
- (IBAction)checkSwitch3Changed:(id)sender {
if (self.switch3.on)
{
self.switch1.on = NO;
self.switch2.on = NO;
self.switch4.on = NO;
}
...
}
- (IBAction)checkSwitch4Changed:(id)sender {
if (self.switch4.on)
{
self.switch1.on = NO;
self.switch2.on = NO;
self.switch3.on = NO;
}
...
}
这是我在 .h 中的代码
@property (weak, nonatomic) IBOutlet UISwitch *switch1;
@property (weak, nonatomic) IBOutlet UISwitch *switch2;
- (IBAction)checkSwitch1Changed:(id)sender;
- (IBAction)checkSwitch2Changed:(id)sender;
//********
@property (weak, nonatomic) IBOutlet UISwitch *switch3;
@property (weak, nonatomic) IBOutlet UISwitch *switch4;
- (IBAction)checkSwitch3Changed:(id)sender;
- (IBAction)checkSwitch4Changed:(id)sender;
如果我使用该视图控制器发送数据,这实际上有效并返回 true。但我试图从另一个视图控制器访问这些数据,它发回错误。我使用此代码从其他视图控制器访问数据。
//import data from SetupController'
SetupController *setupController = [[SetupController alloc] initWithNibName:@"SetupController" bundle:nil];
...
Firebase *postRef = [savedRef childByAppendingPath: @"Switches"];
NSDictionary *post1 = @{
@"Check1": @(setupController.self.switch1.on),
【问题讨论】:
-
你确定
self.switch1不是nil? -
如何检查?我将交换机连接到数据库,每次我在我的 firebase 数据库中请求交换机状态时,它总是返回 false。 @rmaddy
-
当一个开关打开时,您的所有 IBAction 都会被调用吗?您将其命名为 checkSwitch1Changed,因此当您打开 switch2 并关闭 switch1 时,使用该命名方案,应该调用 checkSwitch1Changed、checkSwitch3Changed 等......对吗?
-
不,他们都是独立的。我刚刚更新了代码以进一步澄清它。 @MSU_Bulldog
-
您是否尝试过通过调用 setOn:animated 来打开/关闭它? developer.apple.com/library/ios/documentation/UIKit/Reference/…: isOn 是一个自定义设置器属性,您需要调用它来设置开关的打开或关闭
标签: ios objective-c uiswitch