ios - 向 UITableViewCell : storyboard vs code 添加一个开关

标签 ios swift uitableview

我想在我的设置 View Controller 中添加一个 UISwitch。该开关看起来与 iOS 上“设置”应用程序中的飞行模式开关相同。我不确定什么是最好的实现方式。

我正在选择:

  1. 在代码中添加一个 UISwitch
  2. 在 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/

相关文章:

android - 通过 Facebook API 将消息发布到别人的墙( friend 的墙)?

ios - 设置一个 IBObjects 数组

ios - 为 iOS 应用程序创建数据库

ios - Linux 上读取/转储 MachO 文件的工具?

ios - 为什么从 viewWillAppear 或 viewDidAppear 中显示和隐藏 View 不起作用

ios - TableView 从 : numberOfRowsInSection 中的最后一部分开始

arrays - type 'any' has no subscript members error with map

ios - 当我的 slider 拇指在中间时,为什么没有播放音乐音量?

ios - 缓存以 NSIndexPath 为键返回错误的 objectForKey

ios - 适用于 UITableViewDataSource 的数据表示