我有一个UISegmentedControl,它有6个我在Interface Builder中创建的段。我在更改值时调用以下方法:
-(IBAction)segmentedChartButtonChanged:(id)sender
{
switch (self.segmentedChartButton.selectedSegmentIndex) {
case 0:
NSLog(@"5d selected. Index: %d", self.segmentedChartButton.selectedSegmentIndex);
break;
case 1:
NSLog(@"3m selected. Index: %d", self.segmentedChartButton.selectedSegmentIndex);
break;
default:
break;
}
}
每当我更改细分时,selectedSegmentIndex is always 0
。这是为什么?
答案 0 :(得分:20)
IBOutlet
很可能没有连接。检查一下。您还可以使用传递给方法的sender
。
UISegmentedControl * segmentedControl = (UISegmentedControl *)sender;
switch (segmentedControl.selectedSegmentIndex) {
....
答案 1 :(得分:4)
你好这样试试
-(IBAction)segmentedChartButtonChanged:(id)sender
{
UISegmentedControl *segment=(UISegmentedControl*)sender;
switch (segment.selectedSegmentIndex) {
case 0:
NSLog(@"5d selected. Index: %d", self.segmentedChartButton.selectedSegmentIndex);
break;
case 1:
NSLog(@"3m selected. Index: %d", self.segmentedChartButton.selectedSegmentIndex);
break;
default:
break;
}
}
答案 2 :(得分:0)
为UISegmentedControl取一个插座,然后像这样处理,
if (self.paymentSegmentButton.selectedSegmentIndex ==0)
{
NSLog(@"first view ");
}
else
{
NSLog(@"payment initiation");
addVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"secondView"];
[self.navigationController pushViewController:addVC animated:YES];
}
我希望它有助于某人。