ios - 我想将 UISegmentedControl.selectedSegmentIndex 保存在核心数据中 - 但如何

标签 ios core-data uisegmentedcontrol

我使用分段控件作为联系人应用程序的一种单选按钮系统。 有三个按钮,问题是联系人是否是现有客户。 按钮的名称为“否”、"is"、“其他部门”

我在开发 iOS 方面完全是新手,而这个正在拔掉我最后的白发

我希望能够将 selectedSegmentIndex 值保存在核心数据的数据库中,这样我就可以在下一个 View 上正确显示状态 - 但我真的很难理解如何保存该值:

if (self.existCustSelection.selectedSegmentIndex == 2)
    [self.currentContact setExistCust:[existCustSelection stringWithFormat:@"%d", @"2"]];

- 这会产生

的编译错误

No visible @Interface for 'UISegmentedControl' declares the selector 'stringWithFormat:'

我确实明白 - 我的问题是,我想设置 existCust 而不是 existCustSelection,但无论出于何种原因,我在编写过程中都没有得到该选项代码。

我的阅读是这样的,没有给出错误或警告:

if ([currentContact existCust] == @"2") {
            self.existCustSelection.selectedSegmentIndex = 2;
    }

希望有一个很好的解决方案吗? :)

提前致谢, 米奇

最佳答案

existCust 必须是字符串吗?如果它只存储数字,请考虑使用 NSNumber:

NSNumber* custNumber = [NSNumber numberWithInteger:existCustSelection.selectedSegmentIndex];

如果您确实需要字符串,请按以下步骤使用 stringWithFormat 将索引转换为字符串:

NSString* custString = [NSString stringWithFormat:@"%d", existCustSelection.selectedSegmentIndex];

stringWithFormat 的调用存在两个问题:

  1. stringWithFormatNSString 的类方法。您要求 UISegmentedControl 实例返回一个字符串,但它不知道如何执行此操作。
  2. 在 stringWithFormat 中请求 %d 后,第一个参数必须是整数,例如 2,而不是字符串 @"2".

关于ios - 我想将 UISegmentedControl.selectedSegmentIndex 保存在核心数据中 - 但如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10044942/

相关文章:

swift - 如何检索满足条件 swift 2 的 CoreData

ios - 带有 UISegmentedControl AutoLayout 的 UIToolbar(/全宽)

iphone - 多行的 UISegmentedControl 文本?

iphone - 使用 Core Graphics 以 30-60 fps 的速度手动移动/旋转东西

iphone - 如何释放整个 UINavigationController

ios - 如何在 AVAudioRecorder 中控制 Opus 比特率

html - 带有输入的 Flexbox 移动页脚导致虚拟键盘间隙

ios - iOS 上的 Google Analytics(分析)警告

iphone - 用于排除给定 NSManagedObject 的 NSPredicate 语法

swift - 刷新时出现 fatal error : Array index out of range. Swift