ios - 检测 uiswitch 更改并相应地运行代码

标签 ios xcode uiswitch

我想要三个不同的开关。当我点击第一个开关时,我想让另外两个开关关闭。接下来的两个也是如此。我想感知它是否已更改,然后运行该开关的代码。我在 .h 文件中将开关声明为 socket 并正确连接它们。这可能吗?如果是这样,我该怎么做?我尝试了几件事,但为了工作,我只需要在其中一个开关发生变化时运行代码。否则逻辑无法正常工作。

最佳答案

您可以像连接 UIButton 一样将 UISwitch 连接到操作方法,因此在 .xib 文件中连接您的开关并将此代码添加到您的实现中文件:

-(void)switchChanged {
    //Common code that is performed for all switches
}

-(IBAction)switch1:(id)sender {    //Hook up to switch 1 in your xib
    [self switchChanged];
    //Insert switch 1 specific code here
}

-(IBAction)switch2:(id)sender {    //Hook up to switch 2 in your xib
    [self switchChanged];
    //Insert switch 2 specific code here
}

-(IBAction)switch3:(id)sender {    //Hook up to switch 3 in your xib
    [self switchChanged];
    //Insert switch 3 specific code here
}

关于ios - 检测 uiswitch 更改并相应地运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25374379/

相关文章:

ios - 无法让 UISwitch 约束工作 - swift 4

ios - 当用户在详细 View Controller 中点击 `Back` 时触发一个函数

ios - SceneKit - 什么 orthographicProjection 用于 1 :1 points to SceneKit position ratio

ios - Swift - 如何获取当前时间(以秒为单位)?

xcode - 在 iOS 4.2 中更改 UISwitch 文本

ios - swift - 如何保存开关,即使我更改 TableViews

ios - 将 AVAudioPCMBuffer 转换为 NSData

ios - 在IOS中使用缓存 list 实现Progressive Web App的缓存网页

ios - 为测试飞行创建企业 .ipa 时使用什么 url?

ios - 在 Swift 3 中保存 TableView 的数据