Ios 根据所选位置获取要更改的图像 导入UIKit 类ViewController:UIViewController{ @IBOutlet弱var firstSegControl:UISegmentedControl! @IBOutlet弱var secondSegControl:UISegmentedControl! @IBMOutlet弱变量msgLabel:UILabel! @IBOutlet弱var位置ImageView:UIImageView! var=curPlace=“基内兰” //这将列出所有的城市 //还有所有的地方。 让城市:[字符串]=[ “列克星敦”, “成都”, “芝加哥”, “香港”, ] 让位置:[[String]]=[ [“基尼兰”、“植物园”], [“熊猫”、“步行街”], [“千年”、“空中甲板”、“巡航”、“天文馆”], [“迪士尼乐园”、“海洋公园”、“山顶”] ] //这是允许两个分段控制器同时工作的动作功能。 @iAction func LocationChoiceSimade(\发送方:UISegmentedControl){ updateSecondSegControl(sender.selectedSegmentIndex) } @iAction func LabelChoiceSimade(uu发送方:UISegmentedControl){ curPlace=sender.titleForSegment(在:sender.selectedSegmentIndex)! msgLabel.text=(curPlace) } 重写func viewDidLoad(){ super.viewDidLoad() //加载视图后执行任何其他设置。 firstSegControl.removeAllSegments()文件 因为我在虚空中{ secondSegControl.removeAllSegments() 对于0中的i..
开始时,您需要考虑您的数据 大概,您要么拥有一个数据库,要么从远程服务器获取信息 您可能有如下数据结构:Ios 根据所选位置获取要更改的图像 导入UIKit 类ViewController:UIViewController{ @IBOutlet弱var firstSegControl:UISegmentedControl! @IBOutlet弱var secondSegControl:UISegmentedControl! @IBMOutlet弱变量msgLabel:UILabel! @IBOutlet弱var位置ImageView:UIImageView! var=curPlace=“基内兰” //这将列出所有的城市 //还有所有的地方。 让城市:[字符串]=[ “列克星敦”, “成都”, “芝加哥”, “香港”, ] 让位置:[[String]]=[ [“基尼兰”、“植物园”], [“熊猫”、“步行街”], [“千年”、“空中甲板”、“巡航”、“天文馆”], [“迪士尼乐园”、“海洋公园”、“山顶”] ] //这是允许两个分段控制器同时工作的动作功能。 @iAction func LocationChoiceSimade(\发送方:UISegmentedControl){ updateSecondSegControl(sender.selectedSegmentIndex) } @iAction func LabelChoiceSimade(uu发送方:UISegmentedControl){ curPlace=sender.titleForSegment(在:sender.selectedSegmentIndex)! msgLabel.text=(curPlace) } 重写func viewDidLoad(){ super.viewDidLoad() //加载视图后执行任何其他设置。 firstSegControl.removeAllSegments()文件 因为我在虚空中{ secondSegControl.removeAllSegments() 对于0中的i..,ios,swift,xcode,Ios,Swift,Xcode,开始时,您需要考虑您的数据 大概,您要么拥有一个数据库,要么从远程服务器获取信息 您可能有如下数据结构: import UIKit class ViewController: UIViewController { @IBOutlet weak var firstSegControl: UISegmentedControl! @IBOutlet weak var secondSegControl: UISegmentedControl! @IBOutlet weak va
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
}
}
然后,您将创建一个数据数组:
struct Place {
var placeName: String = ""
var imgName: String = ""
}
struct City {
var cityName: String = ""
var places: [Place] = []
}
您将填充数组:
var cities: [City] = []
现在,要填充第一个分段控件:
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
firstSegControl.removeAllSegments()
for i in 0..<cities.count {
let aCity = cities[i]
firstSegControl.insertSegment(withTitle: aCity.cityName, at: i, animated: false)
}
你需要详细解释你的问题。没有人会阅读你的代码,发现你面临的实际问题是什么。详细阐述你的问题。非常感谢!看到的例子对我来说是改变游戏规则的因素。
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
}
let aCity = cities[firstSegControl.selectedSegmentIndex]
let places = aCity.places
let aPlace = places[secondSegControl.selectedSegmentIndex]
if let img = UIImage(named: aPlace.imgName) {
locationImageView.image = img
}