Define click event for UISegmentedControl

前端 未结 8 2396
无人及你
无人及你 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:53

    If I understand your question correctly, you simply have to implement a target-action method (supported by UIControl which is UISegmentedControl's parent class) for the constant UIControlEventValueChanged, exactly like in the example given in UISegmentControl's reference documentation.

    i.e.

    [segmentedControl addTarget:self
                         action:@selector(action:)
               forControlEvents:UIControlEventValueChanged];
    

    used for a message with the following signature:

    - (void)action:(id)sender
    

    or

    [segmentedControl addTarget:self
                         action:@selector(action:forEvent:)
               forControlEvents:UIControlEventValueChanged];
    

    for

    - (void)action:(id)sender forEvent:(UIEvent *)event
    

    or

    [segmentedControl addTarget:self
                         action:@selector(action)
               forControlEvents:UIControlEventValueChanged];
    

    for the simplest method:

    - (void)action
    

    which are standard types of target-action selectors used in UIKit.

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

    You can attach a handler in IB to the value changed event: UIControlEventValueChanged

    or you can do it in code:

    [segmentedControl addTarget:self
                         action:@selector(action:)
               forControlEvents:UIControlEventValueChanged];
    
    0 讨论(0)
提交回复
热议问题