I want UISegmentedControl (Swift2) to work with other segmented controls.

Asked 2 years ago, Updated 2 years ago, 129 views

When I tap one segmented control, I want to switch other segmented controls accordingly (only display). Is it possible? I tried the name of "self.segmentedControl.selectedSegmentIndex=1", but it didn't work.Thank you for your cooperation.

swift swift2

2022-09-30 21:14

1 Answers

Based on what you said in the comments, I would like to write it in a conclusive way.

  • UISegmentedControl Properties selectedSegmentedIndex are correctly oriented.
  • Value of type' (UISegmentedControl)'->() has no member 'selectedSegmentedIndex' error is most likely incorrectly declared on self.xxx.

For your information, I will list all the code for the ViewController that I created for verification purposes.

import UIKit

classViewController:UIViewController {

    @IBOutlet weak var segmentedControl:UISegmentedControl!

    @ IBAction func buttonPressed (sender: UIButton) {
        let index = sender.tag
        self.segmentedControl.selectedSegmentIndex=index
    }

    @ IBAction func resetPressed(_:AnyObject){
        self.segmentedControl.selectedSegmentIndex=-1
    }
}

InterfaceBuilder has four UISegmentedControl (with three segments) and four UIButton (three of which have tag values of 0,1,2 and one for reset) connected to each @IBOutlet, @IBAction.With this much code, we have verified that UISegmentedControl can be operated by simply pressing the button without directly operating it.

If you believe that this should succeed, you may easily notice a mistake that you have not noticed before, so you can review it so that you can solve it yourself. If you are not sure, please delete the extra code until you can reproduce the error and add the full code to the question.(You should be able to edit your own questions.)


2022-09-30 21:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.