iOS Development Swift-6-Dark Mode, Classes and Objects, MVC Mode, Pop-up Boxes, Closures-Fun Quiz App

1. Create a quiz app project

 2. Create a question text, centered horizontally.

Create two buttons, blue and red, and place them in the Stack View. Give the StackView a horizontal center constraint and a bottom constraint, and set the distance between the two buttons to 20.

 Set the relationship between progress bar view and safe View to equal width. Set their ratio to 1:13.

 3. Add dark mode adaptation to the system (only for iOS13 and above)

Added a new color profile to Assets.

 Sets the colors separately for light and dark.

 Select all the text and set it to a color named Color.

 Effect:

 4. Use the MVC idea to code.

Create a new .swift file, Question.swift:

import Foundation

class Question {
    var text: String
    var answer: Bool
    init(text: String, answer: Bool){
        self.text = text
        self.answer = answer
    }
}

let queastions = [
    Question(text: "马云是世界首富", answer: false),
    Question(text: "刘强东最早是在中关村卖光盘的", answer: true),
    Question(text: "苹果可以吃", answer: true),
    Question(text: "只要坚持下去就能学好代码", answer: true),
    Question(text: "王思聪是80后", answer: true),
    Question(text: "在国内可以正常访问google", answer: false),
    Question(text: "敲完一万行代码可以变身编程高手", answer: true),
    Question(text: "撒贝宁说清华还行", answer: false),
    Question(text: "一直学习变大牛", answer: true),
    Question(text: "安卓也很好使", answer: true),
    Question(text: "优酷比b站牛", answer: false),
    Question(text: "上班可以摸鱼吗", answer: false),
    Question(text: "这狗iOS系统真的没windows好用啊", answer: true)
]

ViewController.swift:

import UIKit

class ViewController: UIViewController {
    var questionIndex = 0
    
    @IBOutlet weak var questionLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        questionLabel.text = queastions[0].text
    }
    
    @IBAction func answerPressed(_ sender: UIButton) {
        if sender.tag == 1 {
            if queastions[questionIndex].answer == true{
                print("huidazhengque")
            }else{
                print("huidacuowu")
            }
        }else{
            if queastions[questionIndex].answer == true{
                print("huidacuowu")
            }else{
                print("huidazhengque")
            }
        }
        questionIndex += 1
        if questionIndex <= 12{
            questionLabel.text = queastions[questionIndex].text
        }else{
            questionIndex = 0
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

 5. Create a pop-up window

ViewController.swift:

import UIKit

class ViewController: UIViewController {
    var questionIndex = 0
    
    @IBOutlet weak var questionLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        questionLabel.text = queastions[0].text
    }
    
    @IBAction func answerPressed(_ sender: UIButton) {
        if sender.tag == 1 {
            if queastions[questionIndex].answer == true{
                print("huidazhengque")
            }else{
                print("huidacuowu")
            }
        }else{
            if queastions[questionIndex].answer == true{
                print("huidacuowu")
            }else{
                print("huidazhengque")
            }
        }
        questionIndex += 1
        if questionIndex <= 12{
            questionLabel.text = queastions[questionIndex].text
        }else{
            let alert = UIAlertController(title: "漂亮!", message: "您已经完成了所有问题,要再来一遍吗?", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "再来一遍", style: .default, handler: { _ in
             
            }))
            self.present(alert, animated: true)
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

 6. Start the test

 7. Perfect again, organize the code.

ViewController.swift:

import UIKit


class ViewController: UIViewController {
    
    var questionIndex = 0
    
    @IBOutlet weak var questionLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        questionLabel.text = queastions[0].text
    }
    
    @IBAction func answerPressed(_ sender: UIButton) {
        if sender.tag == 1 {
            if queastions[questionIndex].answer == true{
                print("huidazhengque")
            }else{
                print("huidacuowu")
            }
        }else{
            if queastions[questionIndex].answer == true{
                print("huidacuowu")
            }else{
                print("huidazhengque")
            }
        }
        questionIndex += 1
        
        nextQuestion()
        
    }
    
    func nextQuestion(){
        if questionIndex <= 12{
            questionLabel.text = queastions[questionIndex].text
        }else{
            questionIndex = 0
            let alert = UIAlertController(title: "漂亮!", message: "您已经完成了所有问题,要再来一遍吗?", preferredStyle: .alert)
            let action = UIAlertAction(title: "再来一遍", style: .default, handler: { _ in
                self.questionLabel.text = queastions[0].text
            })
            alert.addAction(action)
            //
            present(alert, animated: true)
            
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

Guess you like

Origin blog.csdn.net/LYly_B/article/details/132614753