我有一个分段控件,可以在我的一个View Controllers上改变5个图像(见下图),但是当有人进入Casting选项卡时,我希望立即选择Segment 0。目前只有当我进入第1段或第2段然后回来时它才被选中,之后它显然可以正常工作。
在行为我已启用并选中的属性检查器中,我甚至尝试过选中状态,但总是相同的结果,所以我不确定我哪里出错了。我可以通过向我的ViewDidLoad添加5个图像的代码来获得视觉修复,将其中的四个设置为.isHidden = true,将其中一个设置为.isHidden = false,但这显然无法解决我的问题。
这是我的代码:
import UIKit
class CastingViewController: UIViewController {
let runesArray = [Rune(runeName: "Fehu", runeImage: UIImage(named: ("Fehu.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Uruz", runeImage: UIImage(named: ("Uruz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Thurisaz", runeImage: UIImage(named: ("Thurisaz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Ansuz", runeImage: UIImage(named: ("Ansuz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Raidho", runeImage: UIImage(named: ("Raidho.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Kenaz", runeImage: UIImage(named: ("Kenaz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Gebo", runeImage: UIImage(named: ("Gebo.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Wunjo", runeImage: UIImage(named: ("Wunjo.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Hagalaz", runeImage: UIImage(named: ("Hagalaz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Nauthiz", runeImage: UIImage(named: ("Nauthiz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Isa", runeImage: UIImage(named: ("Isa.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Jera", runeImage: UIImage(named: ("Jera.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Eihwaz", runeImage: UIImage(named: ("Eihwaz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Perthro", runeImage: UIImage(named: ("Perthro.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Algiz", runeImage: UIImage(named: ("Algiz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Sowilo", runeImage: UIImage(named: ("Sowilo.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Tiwaz", runeImage: UIImage(named: ("Tiwaz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Berkano", runeImage: UIImage(named: ("Berkano.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Ehwaz", runeImage: UIImage(named: ("Ehwaz.png"))!, runeDescription: "(Description goes here."),
Rune(runeName: "Mannaz", runeImage: UIImage(named: ("Mannaz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Laguz", runeImage: UIImage(named: ("Laguz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Ingwaz", runeImage: UIImage(named: ("Ingwaz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Dagaz", runeImage: UIImage(named: ("Dagaz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Othala", runeImage: UIImage(named: ("Othala.png"))!, runeDescription: "Description goes here.")]
var runesCastArray: [UIImage] = [] // Array to save Rune images for 3 or 5 Rune Cast
@IBOutlet weak var segmentedControl: UISegmentedControl!
@IBOutlet weak var runeOne: UIImageView!
@IBOutlet weak var runeTwo: UIImageView!
@IBOutlet weak var runeThree: UIImageView!
@IBOutlet weak var runeFour: UIImageView!
@IBOutlet weak var runeFive: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
segmentedControl.selectedSegmentIndex = 0
}
var runeCast = 0 // Need to check if this will work in a Switch statement
var storedRuneOne: UIImage!
var storedRuneTwo: UIImage!
var storedRuneThree: UIImage!
var storedRuneFour: UIImage!
var storedRuneFive: UIImage!
func oneRuneCast() {
runeCast = 1
runeOne.isHidden = true
runeTwo.isHidden = true
runeThree.isHidden = true
runeFour.isHidden = true
runeFive.isHidden = false
}
func threeRuneCast() {
runeCast = 3
runeOne.isHidden = true
runeTwo.isHidden = false
runeThree.isHidden = true
runeFour.isHidden = false
runeFive.isHidden = false
}
func fiveRuneCast() {
runeCast = 5
runeOne.isHidden = false
runeTwo.isHidden = false
runeThree.isHidden = false
runeFour.isHidden = false
runeFive.isHidden = false
}
@IBAction func castType(_ sender: Any) {
switch segmentedControl.selectedSegmentIndex {
case 0:
oneRuneCast()
case 1:
threeRuneCast()
case 2:
fiveRuneCast()
default: break
}
}
@IBAction func castButtonPressed(_ sender: Any) {
if runeCast == 1 {
let randomRunes = runesArray[Int(arc4random_uniform(UInt32(runesArray.count)))]
runeFive.image = randomRunes.runeImage
} else if runeCast == 3 {
let randomRunes = runesArray[Int(arc4random_uniform(UInt32(runesArray.count)))]
runeTwo.image = randomRunes.runeImage
runeFour.image = randomRunes.runeImage
runeFive.image = randomRunes.runeImage
} else if runeCast == 5 {
let randomRunes = runesArray[Int(arc4random_uniform(UInt32(runesArray.count)))]
runeOne.image = randomRunes.runeImage
runeTwo.image = randomRunes.runeImage
runeThree.image = randomRunes.runeImage
runeFour.image = randomRunes.runeImage
runeFive.image = randomRunes.runeImage
}
}
}
答案 0 :(得分:3)
以编程方式在viewDidAppear或viewDidLoad中设置段索引(达到您的应用程序要求)
override func viewDidAppear() {
super.viewDidAppear()
segmentedControl.selectedSegmentIndex = 0
}
答案 1 :(得分:0)
您需要在视图出现时重置所选的段索引(例如在viewWillAppear
中),但以编程方式更改所选的段不会调用操作处理程序;你需要明确地调用它。
您还可以将UIImageViews
放入数组中来简化代码;
class CastingViewController: UIViewController {
let runesArray = [Rune(runeName: "Fehu", runeImage: UIImage(named: ("Fehu.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Uruz", runeImage: UIImage(named: ("Uruz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Thurisaz", runeImage: UIImage(named: ("Thurisaz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Ansuz", runeImage: UIImage(named: ("Ansuz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Raidho", runeImage: UIImage(named: ("Raidho.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Kenaz", runeImage: UIImage(named: ("Kenaz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Gebo", runeImage: UIImage(named: ("Gebo.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Wunjo", runeImage: UIImage(named: ("Wunjo.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Hagalaz", runeImage: UIImage(named: ("Hagalaz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Nauthiz", runeImage: UIImage(named: ("Nauthiz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Isa", runeImage: UIImage(named: ("Isa.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Jera", runeImage: UIImage(named: ("Jera.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Eihwaz", runeImage: UIImage(named: ("Eihwaz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Perthro", runeImage: UIImage(named: ("Perthro.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Algiz", runeImage: UIImage(named: ("Algiz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Sowilo", runeImage: UIImage(named: ("Sowilo.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Tiwaz", runeImage: UIImage(named: ("Tiwaz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Berkano", runeImage: UIImage(named: ("Berkano.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Ehwaz", runeImage: UIImage(named: ("Ehwaz.png"))!, runeDescription: "(Description goes here."),
Rune(runeName: "Mannaz", runeImage: UIImage(named: ("Mannaz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Laguz", runeImage: UIImage(named: ("Laguz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Ingwaz", runeImage: UIImage(named: ("Ingwaz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Dagaz", runeImage: UIImage(named: ("Dagaz.png"))!, runeDescription: "Description goes here."),
Rune(runeName: "Othala", runeImage: UIImage(named: ("Othala.png"))!, runeDescription: "Description goes here.")]
var runesCastArray: [UIImage] = [] // Array to save Rune images for 3 or 5 Rune Cast
@IBOutlet weak var segmentedControl: UISegmentedControl!
@IBOutlet weak var runeOne: UIImageView!
@IBOutlet weak var runeTwo: UIImageView!
@IBOutlet weak var runeThree: UIImageView!
@IBOutlet weak var runeFour: UIImageView!
@IBOutlet weak var runeFive: UIImageView!
var runeImageViews:[UIImageView]!
var runeCount = 1
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.runeImageViews=[runeFive,runeFour,runeThree,runeTwo,runeOne]
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
segmentedControl.selectedSegmentIndex = 0
self.runeCount = 1
self.showHideRuneViews()
}
func showHideRuneViews(clearImage: Bool=true) {
for i in 0..<self.runeImageViews.count {
let imageView = self.runeImageViews[i]
if i < self.runeCount {
imageView.isHidden = false
} else {
imageView.isHidden = true
}
if clearImage {
imageView.image = nil
}
}
}
func runeCast() {
for i in 0..<self.runeImageViews.count.reversed() {
let imageView = self.runeImageViews[i]
if i < self.runeCount {
imageView.image = runesArray[Int(arc4random_uniform(UInt32(runesArray.count)))]
} else {
imageView.image = nil
}
}
@IBAction func castType(_ sender: Any) {
switch segmentedControl.selectedSegmentIndex {
case 0:
self.runeCount = 1
case 1:
self.runeCount = 3
case 2:
self.runeCount = 5
default: break
}
self.showHideRuneViews()
}
@IBAction func castButtonPressed(_ sender: Any) {
self.runeCast()
}
}