等效于 UISwtich 中的 UISegmentedControl selectedSegmentIndex- Objective C

Equivalent of UISegmentedControl selectedSegmentIndex in UISwtich- Objective C

我正在尝试将 UISegmentedControl 转换为 UISwitch。我成功地检查了开关的状态。

然后我看到了这行代码:

self.mobile3GSegment.selectedSegmentIndex = [mConfigurationService getBoolWithKey:NETWORK_USE_3G];

我试图猜测 UISwitch 是如何做到的。所以...

self.mobile3GSwitch.state = [mConfigurationService getBoolWithKey:NETWORK_USE_3G];

然后我得到这个错误:

Property 'selectedSegmentIndex' not found on object of type UISwitch

我知道 UISwitch 不是一个段并且 returns 是或否。错误不是这里的重点。我想了解的是如何转换代码段以使其适用于 UISwitch。

我现在几乎迷路了。这样做的正确方法是什么?

UISwitch 是二进制的:打开或关闭。它具有 属性 on,而不是选定的段索引,因为没有段。 当您遇到困难时,查看 Apple 的文档总是一个好主意:link.

你必须使用一个简单的函数setOn(假设你返回 bool 的方法是正确的)。

[_mobile3GSwitch setOn:[mConfigurationService getBoolWithKey:NETWORK_USE_3G] animated:NO];