Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 静态UITableViewCell中的UISegmentedControl_Ios_Objective C_Cocoa Touch_Uitableview_Uisegmentedcontrol - Fatal编程技术网

Ios 静态UITableViewCell中的UISegmentedControl

Ios 静态UITableViewCell中的UISegmentedControl,ios,objective-c,cocoa-touch,uitableview,uisegmentedcontrol,Ios,Objective C,Cocoa Touch,Uitableview,Uisegmentedcontrol,我在使用UISegmentedControl时遇到了一个非常奇怪的问题,这种问题在如此简单的接口元素中是不可能出现的。我在我的故事板中为我的tableview设计了一个静态单元格(tableview有8个单元格,其中许多单元格带有界面元素,如步进器和标签,工作正常),其中包含一个UISegmentedControl。我将接口对象链接到以下适当的方法: @property (strong, nonatomic) IBOutlet UISegmentedControl *plantHealthSeg

我在使用
UISegmentedControl
时遇到了一个非常奇怪的问题,这种问题在如此简单的接口元素中是不可能出现的。我在我的故事板中为我的tableview设计了一个静态单元格(tableview有8个单元格,其中许多单元格带有界面元素,如步进器和标签,工作正常),其中包含一个
UISegmentedControl
。我将接口对象链接到以下适当的方法:

@property (strong, nonatomic) IBOutlet UISegmentedControl *plantHealthSegmentedControl;
- (IBAction)plantHealthValueChanged:(id)sender;
奇怪的是,当我在
viewdiload
方法中初始化分段控件时,接口对象没有改变脚本编辑器中设置的默认值。这是用于在我的表视图实现文件中对其进行修改的代码:

plantHealthSegmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Excellent", @"Good", @"Poor", @"Dead", nil]];
plantHealthSegmentedControl.tintColor = gardenGreenColor;
plantHealthSegmentedControl.selectedSegmentIndex = 0;
我的
plantHealthValueChanged
方法如下(它总是将-1记录为所选索引


非常感谢您对这个奇怪问题的任何帮助

问题是您正在重新初始化控件。这抹去了故事板引用,并创建了一个全新的,没有连接到任何东西的引用

你需要摆脱这一行:

plantHealthSegmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Excellent", @"Good", @"Poor", @"Dead", nil]];
我建议直接在interface builder中设置段信息,因为您在那里创建了控件

或者,如果你想在代码中实现它,有几种方法。您可以使用
insertSegmentWithTitle:atIndex:animated:
单独添加它们。但是,如果有任何段来自NIB,您可能需要首先调用
removeAllSegments

plantHealthSegmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Excellent", @"Good", @"Poor", @"Dead", nil]];