UIButton执行UISegment控制操作

时间:2014-10-02 13:33:16

标签: ios uibutton uisegmentedcontrol

我的应用程序中有一个UISegmented控件。我也有一个自定义按钮。我希望这个自定义按钮可以将细分返回到index 0,还可以执行index 0方法。

这是我的分段控件;

- (IBAction)segmentSwitch:(id)sender {
  UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
  NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;

  if (selectedSegment == 0) {
  //perform custom action

  }
  else if(selectedSegment ==1){

  }
  else if (selectedSegment ==2){

 //show custom button


 }

}

我的按钮是;

-(IBAction)buttonAction: (id)sender{

 //deselect index 2 of segmented control

 //go to index 0
 //perform index 0s method
}

我的按钮是否有办法为segmentSwitch执行index 0操作?

2 个答案:

答案 0 :(得分:1)

只需从按钮的目标方法中调用segmenteSwitch方法:

 -(IBAction)buttonAction: (id)sender{

     self.segmentedControl.selectedIndex = 0;
     [self segmentSwitch:self.segmentedControl];
 }

答案 1 :(得分:0)

- (IBAction)buttonAction:(id)sender {
    self.segmentedControl.selectedSegmentIndex = 0;
    [self.segmentedControl sendActionsForControlEvents:UIControlEventValueChanged];
}