使用Swift的多视图控制器

时间:2017-03-30 11:43:15

标签: ios swift multiple-views

My Storyboard 您好我是编程的新手,但我正在尝试创建一个IOS应用程序,我在其中显示事实并教授一个主题,然后在最后做一个测验。我认为几个多视图控制器最适合这个,每个主题都有一个视图控制器,然后是每个测验的视图控制器。 (这是正确的做法吗?)

我还没有开始讨论事实部分,因为我不知道如何在点击按钮时从swift中的数据文件(只有随机数据)按顺序运行一系列事实,(任何想法最好的是什么)解决方案是为了这个?)。在第三个视图控制器,它是我的测验后的教学部分,我试图实现它并通过第二个视图控制器的按钮访问它,它进入第二视图控制器罚款,然后从导航栏返回,但它赢了让我访问第三个视图控制器(我的测验打开),当它尝试加载它崩溃并进入调试屏幕。任何帮助将不胜感激。因为一旦我有一个排序应该更容易做其他人。

    `//  BasicsQuizViewController.swift
    //  Java Fun; Learning How To Code
    //
    //  Created by Adam Brooke on 30/03/2017.

//版权所有©2017 Adam Brooke。版权所有。 //

导入UIKit

class BasicsQuizViewController:UIViewController {

@IBOutlet weak var QuestionLabel: UILabel!
@IBOutlet weak var Button1: UIButton!
@IBOutlet weak var Button2: UIButton!
@IBOutlet weak var Button3: UIButton!
@IBOutlet weak var Button4: UIButton!

var CorrectAnswer = String()

override func viewDidLoad() {
    super.viewDidLoad()

    RandomQuestions()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func RandomQuestions(){
    var RandomNumber = arc4random() % 4
    RandomNumber += 1

    switch(RandomNumber){

    case 1:
        QuestionLabel.text = "Hello World, What i My Name?"
        Button1.setTitle("John", for: UIControlState.normal)
        Button2.setTitle("Boo", for: UIControlState.normal)
        Button3.setTitle("Adam", for: UIControlState.normal)
        Button4.setTitle("Bill", for: UIControlState.normal)
        CorrectAnswer = "2"
        break
    case 2:
        QuestionLabel.text = "What would you like for tea?"
        Button1.setTitle("Curry", for: UIControlState.normal)
        Button2.setTitle("Hot Dog", for: UIControlState.normal)
        Button3.setTitle("Poo", for: UIControlState.normal)
        Button4.setTitle("Mc D's", for: UIControlState.normal)
        CorrectAnswer = "4"

        break
    case 3:
        QuestionLabel.text = "Are you Gay?"
        Button1.setTitle("Yeah", for: UIControlState.normal)
        Button2.setTitle("No", for: UIControlState.normal)
        Button3.setTitle("Maybe", for: UIControlState.normal)
        Button4.setTitle("A Little", for: UIControlState.normal)
        CorrectAnswer = "2"

        break
    case 4:
        QuestionLabel.text = "What sport do you like?"
        Button1.setTitle("Football", for: UIControlState.normal)
        Button2.setTitle("Rugby", for: UIControlState.normal)
        Button3.setTitle("Tennis", for: UIControlState.normal)
        Button4.setTitle("Golf", for: UIControlState.normal)
        CorrectAnswer = "1"

        break
    default:

        break
    }

}


@IBAction func Button1Action(_ sender: Any) {
    if (CorrectAnswer == "1"){

    NSLog("Correct")
}
else{
    NSLog("Sorry You are Wrong")
    }
}
@IBAction func Button2Action(_ sender: Any) {
    if (CorrectAnswer == "2"){

    NSLog("Correct")
}
else{
    NSLog("Sorry You are Wrong")
    }
}

@IBAction func Button3Action(_ sender: Any) {
    if (CorrectAnswer == "3"){

    NSLog("Correct")
}
    else{
        NSLog("Sorry You are Wrong")
    }
}

@IBAction func Button4Action(_ sender: Any) {
    if (CorrectAnswer == "4"){

    NSLog("Correct")
}
    else{
        NSLog("Sorry You are Wrong")
    }
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

} The Errors 1

0 个答案:

没有答案