Define click event for UISegmentedControl

前端 未结 8 2395
无人及你
无人及你 2020-12-13 11:53

I have added a UISegmentedControl in my application. None of the buttons are selected in normal state. I want to implement a button click event when the first

相关标签:
8条回答
  • 2020-12-13 12:28

    try this:

    - (IBAction)segmentSwitch:(UISegmentedControl *)sender {
          NSInteger selectedSegment = sender.selectedSegmentIndex;
    
          if (selectedSegment == 0) {
    
          }
          else{
    
          }
        }
    
    0 讨论(0)
  • 2020-12-13 12:31

    Simple version in swift updated

    func loadControl(){
         self.yourSegmentedControl.addTarget(self, action: #selector(segmentSelected(sender:)), forControlEvents: .valueChanged)
    }
    
    func segmentSelected(sender: UISegmentedControl)
    {
        let index = sender.selectedSegmentIndex
    
        // Do what you want
    }
    
    0 讨论(0)
  • 2020-12-13 12:37

    It's worth noting that the selector has to match a method exactly, but without the actual parameters.

    @selector(action)
        => -(void) action { ..code.. }
    
    @selector(action:) 
        => -(void) action:(id)sender { ..code.. }
    
    @selector(action:forEvent:) 
        => -(void) action:(id)sender forEvent:(UIEvent *)event { ..code.. }
    

    This confused me for the longest time, and it's not quite clear from the earlier answers.

    0 讨论(0)
  • 2020-12-13 12:39

    Simple like that, you need to make the action using the storyboard to the view controller in order to make this action to work.

    - (IBAction)searchSegmentedControl:(UISegmentedControl *)sender
    {
        switch (sender.selectedSegmentIndex) {
            case 0:
                NSLog(@"First was selected");
                break;
            case 1:
                NSLog(@"Second was selected");
                break;
            case 2:
                NSLog(@"Third was selected");
                break;
            default:
                break;
        }
    }
    
    0 讨论(0)
  • 2020-12-13 12:43

    for SWIFT use this:

    segmentedControl.addTarget( self, action: "segmentSelected:", forControlEvents: UIControlEvents.ValueChanged )
    

    and implement a method like this:

    func segmentSelected(sender: UISegmentedControl)
    {
        println("selected index: \(sender.selectedSegmentIndex)")
    }
    

    Extend UISegmentedControl if you want to call a specific action when the selected segment is selected:

    class CustomSegmentedControl : UISegmentedControl
    {        
        override init()
        {
            super.init()
        }
    
        override init(frame:CGRect)
        {
            super.init(frame:frame)
        }
    
        required init(coder aDecoder: NSCoder) {
           super.init(coder: aDecoder)
        }
    
        func valueChanged(index:Int)
        {
             // ACTION GOES HERE
        }      
    }
    

    and call your "valueChanged" in "segmentSelected" like this:

    func segmentSelected(sender: CustomSegmentedControl)
    {
        println("selected index: \(sender.selectedSegmentIndex)")
        sender.valueChanged(sender.selectedSegmentIndex)
    }
    
    0 讨论(0)
  • 2020-12-13 12:48

    Try this one,

        UISegmentedControl  * travelModeSegment = [[UISegmentedControl alloc] initWithItems:
        [NSArray arrayWithObjects:NSLocalizedString(@"Driving", nil),                 NSLocalizedString(@"Walking", nil), nil]];
        [travelModeSegment setFrame:CGRectMake(9.0f, 0.0f, 302.0f, 45.0f)];
        [cell addSubview:travelModeSegment];
        [travelModeSegment release];
    

    then write an action,

         if (travelModeSegment.selectedSegmentIndex == 0) {
            //write here your action when first item selected
        } else {
            //write here your action when second item selected
        }
    

    I hope it will help you

    0 讨论(0)
提交回复
热议问题