ランダムな質問でクラスを作成しましたが、switchステートメントは終了せず、質問はループし続けます。 4つの質問すべてがランダムに完了したら、これを防止し、別のページを表示するにはどうすればよいですか?あなたは既に尋ねた質問の数のカウンタを維持する必要がスイッチケースを終了する方法
import UIKit
class ThirdViewController: UIViewController {
@IBOutlet weak var Question: UILabel!
@IBOutlet weak var Ans1: UIButton!
@IBOutlet weak var Ans2: UIButton!
@IBOutlet weak var Ans3: UIButton!
@IBOutlet weak var Ans4: UIButton!
@IBOutlet weak var Result: UILabel!
@IBOutlet weak var Next: UIButton!
var correctAns = String()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Hide()
Random()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func Random(){
var RandomNumber = arc4random() % 4
RandomNumber += 1
switch (RandomNumber) {
case 1:
Question.text = "Where does minal lives?"
Ans1.setTitle("dubai", for: UIControlState.normal)
Ans2.setTitle("london", for: UIControlState.normal)
Ans3.setTitle("india", for: UIControlState.normal)
Ans4.setTitle("japan", for: UIControlState.normal)
correctAns = "2"
break
case 2:
Question.text = "What is my name?"
Ans1.setTitle("Sunil", for: UIControlState.normal)
Ans2.setTitle("Harish", for: UIControlState.normal)
Ans3.setTitle("Rohit", for: UIControlState.normal)
Ans4.setTitle("Minal", for: UIControlState.normal)
correctAns = "4"
break
case 3:
Question.text = "How old are you?"
Ans1.setTitle("22", for: UIControlState.normal)
Ans2.setTitle("32", for: UIControlState.normal)
Ans3.setTitle("21", for: UIControlState.normal)
Ans4.setTitle("28", for: UIControlState.normal)
correctAns = "1"
break
case 4:
Question.text = "What are you studying?"
Ans1.setTitle("Computer Science", for: UIControlState.normal)
Ans2.setTitle("Java", for: UIControlState.normal)
Ans3.setTitle("Bio", for: UIControlState.normal)
Ans4.setTitle("Business", for: UIControlState.normal)
correctAns = "3"
break
default:
Result.text = "Finished"
break
}
}
func Hide(){
Result.isHidden = true
Next.isHidden = true
}
func Unhide(){
Result.isHidden = false
Next.isHidden = false
}
@IBAction func Ans1Action(_ sender: Any) {
Unhide()
if (correctAns == "1"){
Result.text = "Correct!"
}
else{
Result.text = "Try Again"
}
}
@IBAction func Ans2Action(_ sender: Any) {
Unhide()
if (correctAns == "2"){
Result.text = "Correct!"
}
else{
Result.text = "Try Again"
}
}
@IBAction func Ans3Action(_ sender: Any) {
Unhide()
if (correctAns == "3"){
Result.text = "Correct!"
}
else{
Result.text = "Try Again"
}
}
@IBAction func Ans4Action(_ sender: Any) {
Unhide()
if (correctAns == "4"){
Result.text = "Correct!"
}
else{
Result.text = "Try Again"
}
}
@IBAction func NextAction(_ sender: Any) {
Random()
Hide()
}
}
Plaeseは[最小限の**、実用的で検証可能な例の作成方法](http://stackoverflow.com/help/mcve)を見ています。 – dfri
関連していませんが、Swiftでは 'switch'と' if'条件の周りに 'break'文と括弧は必要ありません。そしてSwiftの命名規則を考慮してください。 – vadian
あなたは、あなたの(4つの)質問のそれぞれを毎回異なる順序で表示したいと思うのですか? –