【问题标题】:Get the value of a selected item of a SegmentedControl object获取 SegmentedControl 对象的选定项的值
【发布时间】:2011-02-06 11:36:58
【问题描述】:

出于美学原因,我在分段控件上显示货币符号,但我想以 3 个字母返回货币代码。看看我是怎么做到的:

NSArray *itemArray = [NSArray arrayWithObjects: @"EUR", @"USD", @"GBP", nil];
self.writePriceCurrency = [[UISegmentedControl alloc] initWithItems:itemArray];

[self.writePriceCurrency setTitle:@"€" forSegmentAtIndex:0];
[self.writePriceCurrency setTitle:@"$" forSegmentAtIndex:1];
[self.writePriceCurrency setTitle:@"£" forSegmentAtIndex:2];

我知道我可以在函数中使用switch() {} 来捕获值的变化并根据索引返回正确的代码,但这将是硬编码的。我宁愿直接这样做,以便将来可以动态更改分段控件。

您能帮我理解我们如何获取与所选片段对应的项目吗?

【问题讨论】:

    标签: iphone objective-c xcode uisegmentedcontrol


    【解决方案1】:

    也许你可以在键是符号时初始化一个 NSDictionary,然后通过遍历键来初始化段。
    然后,您可以使用 [yourNSDictionary objectForKey:key:] 在捕获值变化的函​​数中获取正确的货币代码。

    【讨论】:

    • 好吧,我用 NSArray 做到了,它工作得很好,但我想我们可能有另一种方法。无论如何,谢谢您的回答
    猜你喜欢
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 2018-08-08
    • 2019-07-29
    • 2017-05-08
    • 1970-01-01
    相关资源
    最近更新 更多