简体   繁体   English

UIButton执行UISegment控件操作

[英]UIButton to perform UISegment Control action

I have a UISegmented control in my app. 我的应用程序中有一个UISegmented控件。 I also have a custom button. 我也有一个自定义按钮。 I would like this custom button to return the Segment back to index 0 but also perform index 0 s method. 我希望此自定义按钮将Segment返回到index 0但也执行index 0 s方法。

Here is my segmented control; 这是我的分段控件;

- (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


 }

}

My button is; 我的按钮是;

-(IBAction)buttonAction: (id)sender{

 //deselect index 2 of segmented control

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

Is there any way for my button to perform the segmentSwitch action for index 0 ? 我的按钮有什么办法可以对index 0执行segmentSwitch操作吗?

Simply call the segmenteSwitch method from the button's target method: 只需从按钮的target方法中调用segmenteSwitch方法即可:

 -(IBAction)buttonAction: (id)sender{

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM