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.启动测试

相关推荐
m0_687399842 分钟前
QT combox 前缀匹配
开发语言·数据库·qt
汤兰月10 分钟前
Python中的观察者模式:从基础到实战
开发语言·python·观察者模式
DieSnowK11 分钟前
[C++][第三方库][httplib]详细讲解
服务器·开发语言·c++·http·第三方库·新手向·httplib
火红的小辣椒16 分钟前
PHP反序列化8(phar反序列化)
开发语言·web安全·php
一颗花生米。3 小时前
深入理解JavaScript 的原型继承
java·开发语言·javascript·原型模式
问道飞鱼3 小时前
Java基础-单例模式的实现
java·开发语言·单例模式
学习使我快乐013 小时前
JS进阶 3——深入面向对象、原型
开发语言·前端·javascript
通信仿真实验室4 小时前
(10)MATLAB莱斯(Rician)衰落信道仿真1
开发语言·matlab
勿语&4 小时前
Element-UI Plus 暗黑主题切换及自定义主题色
开发语言·javascript·ui
吾爱星辰8 小时前
Kotlin 处理字符串和正则表达式(二十一)
java·开发语言·jvm·正则表达式·kotlin