算数app

创建一个新的project
将左边栏整理成MVC——Model,View,Controller
在这里插入图片描述

在原本为空的Model栏里面选中后按Command+N新建一个swift文件
在这里插入图片描述
再创建一个Supporting Files

Main.storyboard 样式:
在这里插入图片描述
其中progressBar是View

整理实现的功能:
当程序运行跳出第一个问题
选择真或假之后:
progressLabel + 1
progressBar + 1 / 13
进入下一题
当回答正确时scoreLabel + 1
每次回答完成都有一个弹出窗口反馈我们是否回答正确
当回答完最后一个问题后,弹出窗口

在Model的Question里面创建问题类

import Foundation

class Question{
    
    let answer: Bool
    let questionText: String
    
    init(text:String,correctAnswer:Bool) {
        questionText = text
        answer = correctAnswer
    }
    
}

在View Controller内随意创建问题

let questions = [
        Question(text: "0 + 1 = 1", correctAnswer: true),
        Question(text: "1 + 1 = 2", correctAnswer: true),
        Question(text: "2 + 2 = 4", correctAnswer: true),
        Question(text: "3 + 2 = 6", correctAnswer: false),
        Question(text: "4 + 4 = 8", correctAnswer: true),
        Question(text: "5 + 5 = 100", correctAnswer: false),
        Question(text: "6 + 7 = 12", correctAnswer: false),
        Question(text: "7 + 1 = 8", correctAnswer: true),
        Question(text: "8 + 5 = 2", correctAnswer: false),
        Question(text: "9 + 1 = 10", correctAnswer: true)
    ]

定义两个变量存储回答到第几个问题和分数

    var questionNum = 0
    var score = 0

在每次程序运行后显示第一个问题

override func viewDidLoad() {
        super.viewDidLoad()
        
        questionLabel.text = questions[0].questionText
        
    }

函数来检查回答的问题是否正确

func checkAnswer(tag:Int) {
        if tag == 1{    //设置的真的tag为1,假的tag为0
            if questions[questionNum].answer == true{
                score += 1       //更改数据
                scoreLabel.text = "总得分:\(score)" //更改UI
            }else{
            }
        }else{
            if questions[questionNum].answer == true{
            }else{
                score += 1  //更改数据
                scoreLabel.text = "总得分:\(score)" //更改UI
            }
        }
    }

函数更新UI

    func updateUI() {
        progressLabel.text = "\(questionNum + 1)/13" //questionNum是从0开始的 所以要 + 1
        progressBar.frame.size.width = (view.frame.width / 13) * CGFloat(questionNum+1)
    }

函数来切换下一题,并且在回答完最后一个问题时出现弹出窗口

  func nextQuestion() {
        questionNum += 1 //更新数据
        if questionNum <= 12{
            //控制器从model处取回了数据,然后反馈给了view---MVC
            questionLabel.text = questions[questionNum].questionText  //更新UI
        }else{
            questionNum = 0 
            score = 0
            
            let alert = UIAlertController(title: "漂亮!", message: "您已经完成了所有问题,要重新来一遍吗?", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "再来一遍", style: .default, handler: { (_) in
                
                self.questionLabel.text = self.questions[0].questionText
                self.scoreLabel.text = "总得分:0"
                self.updateUI()
                
            }))
            
            present(alert, animated: true, completion: nil)
        }
    }

弹出窗口效果:
在这里插入图片描述

按下真或假时调用上述三个函数

@IBAction func answerPressed(_ sender: UIButton) {
        checkAnswer(tag: sender.tag)
        nextQuestion()
        updateUI()
    }

每次回答完成都有一个弹出窗口反馈我们是否回答正确
进入github找到relatedcode/ProgressHUD这个包并且下载下来
在这里插入图片描述
在这里插入图片描述
将ProgressHUD内的三个文件拖到project的Supporting Files里面
并且选择Create Bridging Header
在这里插入图片描述
在刚刚自动生成的Quizzler-Bridging-Header.h文件里面写下列代码

#import "ProgressHUD.h"

之后在需要使用弹出框的checkAnswer函数里面写ProgressHUD.showSuccess()或者ProgressHUD.showError()

func checkAnswer(tag:Int) {
        if tag == 1{
            if questions[questionNum].answer == true{
                score += 1
                scoreLabel.text = "总得分:\(score)"
                ProgressHUD.showSuccess("答对了")
            }else{
                ProgressHUD.showError("答错啦")
            }
        }else{
            if questions[questionNum].answer == true{
                ProgressHUD.showError("答错啦")
            }else{
                score += 1
                scoreLabel.text = "总得分:\(score)"
                ProgressHUD.showSuccess("答对了")
            }
        }
    }

完成,效果如下:
在这里插入图片描述
在这里插入图片描述

最后完成的代码

ViewController:

import UIKit

class ViewController: UIViewController {
    
    let questions = [
        Question(text: "0 + 1 = 1", correctAnswer: true),
        Question(text: "1 + 1 = 2", correctAnswer: true),
        Question(text: "2 + 2 = 4", correctAnswer: true),
        Question(text: "3 + 2 = 6", correctAnswer: false),
        Question(text: "4 + 4 = 8", correctAnswer: true),
        Question(text: "5 + 5 = 100", correctAnswer: false),
        Question(text: "6 + 7 = 12", correctAnswer: false),
        Question(text: "7 + 1 = 8", correctAnswer: true),
        Question(text: "8 + 5 = 2", correctAnswer: false),
        Question(text: "9 + 1 = 10", correctAnswer: true),
        Question(text: "10 + 1 = 11", correctAnswer: true),
        Question(text: "99 + 1 = 101", correctAnswer:false),
        Question(text: "100 + 100 = 100", correctAnswer: false),
        
    ]
    var questionNum = 0
    var score = 0
    
    
    @IBOutlet weak var questionLabel: UILabel!
    @IBOutlet weak var scoreLabel: UILabel!
    @IBOutlet var progressBar: UIView!
    @IBOutlet weak var progressLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        questionLabel.text = questions[0].questionText
        
    }


    @IBAction func answerPressed(_ sender: UIButton) {
        
        checkAnswer(tag: sender.tag)
        nextQuestion()
        updateUI()
    }
    
    
    func updateUI() {
        progressLabel.text = "\(questionNum + 1)/13"
        progressBar.frame.size.width = (view.frame.width / 13) * CGFloat(questionNum+1)
    }
    

    func nextQuestion() {
        questionNum += 1
        if questionNum <= 12{
            //控制器从model处取回了数据,然后反馈给了view---MVC
            questionLabel.text = questions[questionNum].questionText
        }else{
            questionNum = 0
            score = 0
            
            let alert = UIAlertController(title: "漂亮!", message: "您已经完成了所有问题,要重新来一遍吗?", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "再来一遍", style: .default, handler: { (_) in
                
                self.questionLabel.text = self.questions[0].questionText
                self.scoreLabel.text = "总得分:0"
                self.updateUI()
                
            }))
            
            present(alert, animated: true, completion: nil)
        }
    }
    
    
    func checkAnswer(tag:Int) {
        if tag == 1{
            if questions[questionNum].answer == true{
                score += 1
                scoreLabel.text = "总得分:\(score)"
                ProgressHUD.showSuccess("答对了")
            }else{
                ProgressHUD.showError("答错啦")
            }
        }else{
            if questions[questionNum].answer == true{
                ProgressHUD.showError("答错啦")
            }else{
                score += 1
                scoreLabel.text = "总得分:\(score)"
                ProgressHUD.showSuccess("答对了")
            }
        }
    }
    
    
}

Question:
import Foundation

class Question{

let answer: Bool
let questionText: String

init(text:String,correctAnswer:Bool) {
    questionText = text
    answer = correctAnswer
}

}

Quizzler-Bridging-Header:

#import "ProgressHUD.h"
发布了6 篇原创文章 · 获赞 8 · 访问量 353

猜你喜欢

转载自blog.csdn.net/qq_44864362/article/details/100172049
今日推荐