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
96 views
in Technique[技术] by (71.8m points)

ios - Getting image to change based on the selected place

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var firstSegControl: UISegmentedControl!
    @IBOutlet weak var secondSegControl: UISegmentedControl!
    @IBOutlet weak var msgLabel: UILabel!
    
    @IBOutlet weak var locationImageView: UIImageView!
    var = curPlace = "Keeneland"

    //This will list all of the cities
    //and all of the places.
    let cities: [String] = [
            "Lexington",
            "Chengdu",
            "Chicago",
            "Hongkong",
        ]
        let places: [[String]] = [
            ["Keeneland", "Arboretum"],
            ["Panda", "Pedestrian Street"],
            ["Millenium", "Skydeck", "Cruise", "Planetarium"],
            ["Disneyland", "Ocean Park", "The Peak"]
        ]
    //This is the action function that allows the two segmented controlls to work alongside eachother.
    @IBAction func locationChoiceIsMade(_ sender: UISegmentedControl) {
        updateSecondSegControl(sender.selectedSegmentIndex)
    }
    @IBAction func labelChoiceIsMade(_ sender: UISegmentedControl) {
        curPlace = sender.titleForSegment(at: sender.selectedSegmentIndex)!
        msgLabel.text = (curPlace)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        firstSegControl.removeAllSegments()
                for i in 0..<cities.count {
                    firstSegControl.insertSegment(withTitle: cities[i], at: i, animated: false)
                }
                firstSegControl.selectedSegmentIndex = 0
                updateSecondSegControl(0)
    }
    //This will make the second segmented control update based off of the first ones selection.
    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
    }
    
}

Edit: I was able to get the labels to start working, correctly changing based on the place selected. Now, I need to get the picture to change based on the selected place. I am going to keep testing some ideas, any help is appreciated!

Example of the end result

question from:https://stackoverflow.com/questions/66067660/getting-image-to-change-based-on-the-selected-place

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

1 Reply

0 votes
by (71.8m points)

You need to think about your data when you're getting started.

Presumably, you'll either have a database or you'll be pulling the information from a remote server.

You might have a data structure like this:

struct Place {
    var placeName: String = ""
    var imgName: String = ""
}

struct City {
    var cityName: String = ""
    var places: [Place] = []
}

Then, you'd create a data array:

var cities: [City] = []

and you'd fill the array:

    var somePlace: [Place]!
    var aPlace: Place!
    var aCity: City!
    
    // Lexington
    somePlace = []
    
    aPlace = Place(placeName: "Keeneland", imgName: "KeenelandPicture")
    somePlace.append(aPlace)
    
    aPlace = Place(placeName: "Arboretum", imgName: "ArboretumPicture")
    somePlace.append(aPlace)

    aCity = City(cityName: "Lexington", places: somePlace)

    cities.append(aCity)

    
    // Chengdu
    somePlace = []
    
    aPlace = Place(placeName: "Place 1", imgName: "Place1Picture")
    somePlace.append(aPlace)
    
    aPlace = Place(placeName: "Place 2", imgName: "Place2Picture")
    somePlace.append(aPlace)
    
    aCity = City(cityName: "Chengdu", places: somePlace)
    
    cities.append(aCity)
    
    // and so on

Now, to fill your first segmented control:

    firstSegControl.removeAllSegments()
    for i in 0..<cities.count {
        let aCity = cities[i]
        firstSegControl.insertSegment(withTitle: aCity.cityName, at: i, animated: false)
    }

and to fill your second segmented control:

func updateSecondSegControl(_ n: Int) -> Void {
    secondSegControl.removeAllSegments()
    let aCity = cities[n]
    let places = aCity.places
    for i in 0..<places.count {
        let aPlace = places[i]
        secondSegControl.insertSegment(withTitle: aPlace.placeName, at: i, animated: false)
    }
    secondSegControl.selectedSegmentIndex = 0
}

and to set your image when the second segmented control is changed:

    let aCity = cities[firstSegControl.selectedSegmentIndex]
    let places = aCity.places
    let aPlace = places[secondSegControl.selectedSegmentIndex]
    if let img = UIImage(named: aPlace.imgName) {
        locationImageView.image = img
    }

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

...