0

I have a button that I would like to perform different code when each of 3 segmented controls are selected. EX) 如果选择segment1,按下按钮时会执行Code1。如果选择segment2,则按下按钮时将执行Code2。我以前没有使用过分段控制,有人知道怎么做吗?

4

3 回答 3

6

假设您在按钮上设置了目标操作,并且为分段控件设置了 IBOutlet,您可以执行以下操作。

- (IBAction)buttonPressed:(UIButton *)sender
 {
    NSInteger selectedIndex = self.segmentedControl.selectedSegmentIndex;
    switch(selectedIndex)
    {
       case 1: [self methodOne]; break;
       case 2: [self methodTwo]; break;
       case 3: [self methodThree]; break;
       default : break; //Do nothing
    }
 }
于 2013-10-15T13:37:05.397 回答
1

只需以非常简单的方式进行:-如果您使用了段控制的出口,请按照以下方式进行:-

         NSInteger sel=[yourSegmentCntrl       
         selectedSegment];
      If (sel==1)
       {
       //write your code
         }
于 2013-10-14T19:53:35.057 回答
0

因为我最近正在研究这个,所以我想我会添加另一个答案,可以帮助 Swift 开发人员过渡到 Obj-C。这更明确一些,并且与我们在 Swift 中看到的带有 IBOutlets 的内容相匹配。

- (IBAction)segmentedControlAction:(id)sender {

// This property can be a simple primitive. No need for an NSObject like NSInteger.
int selectedIndex = self.segmentedControlOutlet.selectedSegmentIndex;

switch(selectedIndex)
{
    // Simply changing output to label
    case 0: self.label.text = @"Segment 1 was selected";
        break;
    case 1: self.label.text = @"Segment 2 was selected";
        break;
    case 2: self.label.text = @"Segment 3 was selected";
        break;
    case 3: self.label.text = @"Segment 4 was selected";
        break;

    default : break; //Do nothing
}

}

于 2016-02-24T04:59:06.293 回答