【问题标题】:Incompatible pointer types assigning to 'UISegmentedControl' from 'NSString'从“NSString”分配给“UISegmentedControl”的不兼容指针类型
【发布时间】:2014-06-27 13:55:34
【问题描述】:

我的 iOS 应用程序中有一个 UISegmentedControl,根据选择的按钮,我希望 self.itemLocation 被分配一个值“美国”或“全球”。

然而,Xcode 告诉我 NSString 与这种按钮不兼容。如何正确分配这些值?

- (IBAction)itemLocation:(id)sender {

    switch (self.itemLocation.selectedSegmentIndex) {

        default:
        case 0:
            self.itemLocation = @"US";
            break;

        case 1:
            self.itemLocation = @"Worldwide";
            break;
    }

}

【问题讨论】:

  • 你想做什么?更改其中一个片段的标题?
  • 您不能将NSString 分配给UISegmentedControl。查看UISegmentedControl 的文档。有一些方法可以设置段的标题。
  • 这看起来像一个无限循环?
  • @Hyperbole 我不这么认为,因为self.itemLocation 会调用itemLocation,而self.itemLocation = … 会调用setItemLocation:。这个方法叫做itemLocation:,冒号有区别。
  • 这肯定不是无限循环。设置 self.property 肯定不会触发IBAction,除非您已经覆盖了 setter。

标签: ios objective-c uisegmentedcontrol


【解决方案1】:

假设 self.itemLocation 是 UISegmentedControl 那么你可以设置每个段的文本如下:

[self.itemLocation setTitle: @"Title" forSegmentAtIndex: 0];

【讨论】:

    猜你喜欢
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多