Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
106 views
in Technique[技术] by (71.8m points)

ios - How to get a segment control to lead into what the second segment control says

I am trying to write code that will allow one segment control bar to access another segment control bar that then allows you to display the image you are looking for. I am having issues getting the firdt segment control to lead into the second segment control.

This is the code that I have now, I just have the first segment control on there.

Please refer to the figures at the bottom to see what the interface should look.

class ViewController: UIViewController {
    var curLocation = "Lexington"
    
    @IBOutlet weak var msgSegment: UISegmentedControl!
    @IBOutlet weak var KeenelandImageView: UIImageView!
    
    @IBAction func locationChoiceIsMade(_ sender: UISegmentedControl) {
        curLocation = sender.titleForSegment(at: sender.selectedSegmentIndex)!
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
}

How the app interface should look

question from:https://stackoverflow.com/questions/66051404/how-to-get-a-segment-control-to-lead-into-what-the-second-segment-control-says

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When you select a segment on the first control, you want to remove all current segments from the second control and then insert new segments.

Here's a simple example - with a new view controller, add two UISegmentedControl objects, and connect them via IBOutlet. Leave them at the default "First, Second" segments -- we'll be changing those via code:

class DynamicSegmentsViewController: UIViewController {
    
    @IBOutlet var firstSegControl: UISegmentedControl!
    @IBOutlet var secondSegControl: UISegmentedControl!
    
    let cities: [String] = [
        "Lexington",
        "Chengdu",
        "Chicago",
        "Hongkong",
    ]
    let places: [[String]] = [
        ["Keeneland", "Arboretum"],
        ["Place 1", "Place 2", "Place 3"],
        ["Millenium", "Skydeck", "Cruise", "Planetarium"],
        ["HK 1", "HK 2", "HK 3", "HK 4", "HK 5", "HK 6"]
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        firstSegControl.removeAllSegments()
        for i in 0..<cities.count {
            firstSegControl.insertSegment(withTitle: cities[i], at: i, animated: false)
        }
        firstSegControl.selectedSegmentIndex = 0
        updateSecondSegControl(0)
    }
    
    func updateSecondSegControl(_ n: Int) -> Void {
        secondSegControl.removeAllSegments()
        for i in 0..<places[n].count {
            secondSegControl.insertSegment(withTitle: places[n][i], at: i, animated: false)
        }
        secondSegControl.selectedSegmentIndex = 0
    }
    
    @IBAction func locationChoiceIsMade(_ sender: UISegmentedControl) {
        updateSecondSegControl(sender.selectedSegmentIndex)
    }
    
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...