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

相关推荐
@东辰5 分钟前
【golang-技巧】-自定义k8s-operator-by kubebuilder
开发语言·golang·kubernetes
乐悠小码12 分钟前
数据结构------队列(Java语言描述)
java·开发语言·数据结构·链表·队列
史努比.13 分钟前
Pod控制器
java·开发语言
敲敲敲-敲代码22 分钟前
游戏设计:推箱子【easyx图形界面/c语言】
c语言·开发语言·游戏
ROC_bird..31 分钟前
STL - vector的使用和模拟实现
开发语言·c++
MavenTalk37 分钟前
Move开发语言在区块链的开发与应用
开发语言·python·rust·区块链·solidity·move
XiaoLeisj1 小时前
【JavaEE初阶 — 多线程】生产消费模型 & 阻塞队列
java·开发语言·java-ee
2401_840192271 小时前
python基础大杂烩
linux·开发语言·python
@东辰1 小时前
【golang-技巧】- 定时任务 - cron
开发语言·golang·cron
机器人天才一号1 小时前
C#从入门到放弃
开发语言·c#