我想在我的设置 View Controller 中添加一个 UISwitch。该开关看起来与 iOS 上“设置”应用程序中的飞行模式开关相同。我不确定什么是最好的实现方式。
我正在选择:
- 在代码中添加一个 UISwitch
- 在 Storyboard 中创建自定义单元并为开关创建 socket
这是我用于向 UITableViewCell 添加开关的 Swift 代码:
let soundSwitch = UISwitch(frame: CGRectZero)
soundSwitch.addTarget(self, action: "test:", forControlEvents: UIControlEvents.ValueChanged)
// I created an outlet for the cell that will contain a switch
soundCell.accessoryView = tickingSoundSwitch
使用这些解决方案的优点和缺点是什么?
最佳答案
我的建议是使用其中之一
1. Storyboard中的UISwitch和IBAction,如果你使用这种方法,事情会变得很容易,你不需要添加目标和困惑大量代码。
或
2.完全依赖代码,使用这种方法你的UISwitch将不会与storyboard的UI紧密结合。即使将来,如果您想更改 Storyboard 中的 UI 内容,您也不需要对 UISWitch 进行任何更改,即您不需要附加所有这些 IBAction 和 socket 内容,因为您使用的是基于纯代码的方法(前提是您的UISwitch 要求不变)。
关于ios - 向 UITableViewCell : storyboard vs code 添加一个开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31242828/