iOS开发Swift-7-得分,问题序号,约束对象,提示框,类方法与静态方法-趣味问答App

1.根据用户回答计算得分

ViewController.swift:

复制代码
import UIKit


class ViewController: UIViewController {
    
    var questionIndex = 0
    var score = 0
    
    @IBOutlet weak var questionLabel: UILabel!
    @IBOutlet weak var scoreLabel: 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) {
        checkAnswer(sender.tag)
        
        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)
            
        }
    }
    
    func checkAnswer(_ tag: Int){
        if tag == 1 {
            if queastions[questionIndex].answer == true{
                print("huidazhengque")
                score += 1
                scoreLabel.text = "总得分:\(score)"
            }else{
                print("huidacuowu")
            }
        }else{
            if queastions[questionIndex].answer == true{
                print("huidacuowu")
            }else{
                print("huidazhengque")
                score += 1
                scoreLabel.text = "总得分:\(score)"            }
        }    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

2.显示题目序号

ViewController.swift:

复制代码
import UIKit


class ViewController: UIViewController {
    
    var questionIndex = 0
    var score = 0
    
    @IBOutlet weak var questionLabel: UILabel!
    @IBOutlet weak var scoreLabel: UILabel!
    @IBOutlet weak var progressLable: 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) {
        checkAnswer(sender.tag)
        
        questionIndex += 1
        nextQuestion()
        progressLable.text = "\(questionIndex + 1) / 13"
        
    }
    
    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)
            
        }
    }
    
    func checkAnswer(_ tag: Int){
        if tag == 1 {
            if queastions[questionIndex].answer == true{
                print("huidazhengque")
                score += 1
                scoreLabel.text = "总得分:\(score)"
            }else{
                print("huidacuowu")
            }
        }else{
            if queastions[questionIndex].answer == true{
                print("huidacuowu")
            }else{
                print("huidazhengque")
                score += 1
                scoreLabel.text = "总得分:\(score)"            }
        }    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

3.为屏幕进度条更改约束

将1:13的宽度约束拖入ViewController。

因为progressBarView是只读,所以要根据屏幕宽度计算出1/13的宽度,然后加到Constant中。

ViewController.swift:

复制代码
import UIKit


class ViewController: UIViewController {
    
    var questionIndex = 0
    var score = 0
    
    @IBOutlet weak var questionLabel: UILabel!
    @IBOutlet weak var scoreLabel: UILabel!
    @IBOutlet weak var progressLable: UILabel!
    @IBOutlet weak var progressBarViewWidth: NSLayoutConstraint!
    
    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) {
        checkAnswer(sender.tag)
        
        questionIndex += 1
        nextQuestion()
        progressLable.text = "\(questionIndex + 1) / 13"
        progressBarViewWidth.constant = (view.frame.width / 13) * CGFloat(questionIndex)
        
    }
    
    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
                self.scoreLabel.text = "总得分:0"
            })
            alert.addAction(action)
            //
            present(alert, animated: true)
            
        }
    }
    
    func checkAnswer(_ tag: Int){
        if tag == 1 {
            if queastions[questionIndex].answer {
                print("huidazhengque")
                score += 1
                scoreLabel.text = "总得分:\(score)"
            }else{
                print("huidacuowu")
            }
        }else{
            if queastions[questionIndex].answer {
                print("huidacuowu")
            }else{
                print("huidazhengque")
                score += 1
                scoreLabel.text = "总得分:\(score)"            }
        }    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

4.制作弹窗

https://github.com/relatedcode/ProgressHUD

将gitHub上拉的swift文件拖到项目中去。

在ViewController中调用这个swift中的方法。

复制代码
import UIKit


class ViewController: UIViewController {
    
    var questionIndex = 0
    var score = 0
    
    @IBOutlet weak var questionLabel: UILabel!
    @IBOutlet weak var scoreLabel: UILabel!
    @IBOutlet weak var progressLable: UILabel!
    @IBOutlet weak var progressBarViewWidth: NSLayoutConstraint!
    
    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) {
        checkAnswer(sender.tag)
        
        questionIndex += 1
        nextQuestion()
        progressLable.text = "\(questionIndex + 1) / 13"
        progressBarViewWidth.constant = (view.frame.width / 13) * CGFloat(questionIndex)
        
    }
    
    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
                self.scoreLabel.text = "总得分:0"
            })
            alert.addAction(action)
            //
            present(alert, animated: true)
            
        }
    }
    
    func checkAnswer(_ tag: Int){
        if tag == 1 {
            if queastions[questionIndex].answer {
                ProgressHUD.showSucceed("答对了")
                score += 1
                scoreLabel.text = "总得分:\(score)"
            }else{
                ProgressHUD.showError("答错了")
            }
        }else{
            if queastions[questionIndex].answer {
                ProgressHUD.showError("答错了")
            }else{
                ProgressHUD.showSucceed("答对了")
                score += 1
                scoreLabel.text = "总得分:\(score)"            }
        }    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

5.启动测试

相关推荐
呼啦啦啦啦啦啦啦啦3 分钟前
synchronized锁,ReentrantLock 锁
开发语言·
听风的码1 小时前
Vue2封装Axios
开发语言·前端·javascript·vue.js
素界UI设计2 小时前
建筑行业变革:用Three.js构建BIM数据可视化孪生平台
开发语言·javascript·信息可视化
王廷胡_白嫖帝2 小时前
Qt个人通讯录项目开发教程 - 从零开始构建联系人管理系统
开发语言·qt
疯狂的代M夫3 小时前
C++对象的内存布局
开发语言·c++
mit6.8244 小时前
Linux下C#项目构建
开发语言·c#
群联云防护小杜4 小时前
从一次 DDoS 的“死亡回放”看现代攻击链的进化
开发语言·python·linq
霸敛4 小时前
好家园房产中介网后台管理完整(python+flask+mysql)
开发语言·python·flask
Momentary_SixthSense4 小时前
RESP协议
java·开发语言·javascript·redis·后端·python·mysql
only-lucky5 小时前
C++中的 Eigen库使用
开发语言·c++