iOS开发Swift-6-深色模式,类与对象,MVC模式,弹出框,闭包-趣味问答App

news/2024/7/20 20:09:38 标签: ios, swift, mvc, 开发语言

1.创建趣味问答App项目

 2.创建一个问题文本,水平居中约束。

创建蓝、红两个按钮,放入Stack View中,给StackView水平居中约束,下边约束,设置两按钮间距为20.

 设置进度条view与safe View关系为equal width。设置他们的比例为1:13.

 3.为系统增加深色模式适配(仅限iOS13以上版本)

为Assets中新增新的颜色配置。

 分别为浅色和深色设置颜色。

 选中所有文字,设置为名叫Color的颜色。

 效果:

 4.利用MVC思想编码。

创建新.swift文件,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.创建弹窗

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

 7.完善再来一遍,整理代码。

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.
    }

}

http://www.niftyadmin.cn/n/4993387.html

相关文章

电商数据监测的原因是什么

电商数据包含主流电商平台&#xff0c;如淘宝、天猫、京东、拼多多等&#xff0c;也包括抖音、快手等直播平台&#xff0c;另外外卖平台的数据、小时达平台的数据&#xff0c;都可称之为电商数据&#xff0c;电商数据其实也是品牌的线上产品链接&#xff0c;监测产品链接&#…

AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘

原因 pytorch版本问题 解决办法 顺着路径找到文件upsampling.py并打开&#xff0c;修改里面的代码即可解决&#xff0c; 这个方式不需要重新安装pytorch YOLOV5 训练好模型测试时出现问题&#xff1a;AttributeError: ‘Upsample‘ object has no attribute ‘recompute_sc…

Agile Management

Agile Management 敏捷管理

JUC并发编程---Lock锁

文章目录 什么是Locksynchronized加锁和Lock加锁代码示例synchronized使用Lock加锁 公平锁和非公平锁公平锁&#xff1a;非公平锁&#xff1a;Lock和Synchronized的区别 synchronized 版的生产者和消费者Lock 版的生产者和消费者生产者和消费者出现的问题Condition精准通知和唤…

python print格式化输出

在 Python 中&#xff0c;以 f 或 F 前缀开始的字符串表示格式化字符串字面量&#xff0c;通常称为 “f-string”。从 Python 3.6 开始引入&#xff0c;它们是一种在字符串中嵌入表达式的新方法。这些表达式在运行时会被评估&#xff0c;然后使用 {} 将它们插入到字符串中。 这…

Ubuntu 22.04.2 LTS 安装python3.6后报错No module named ‘ufw‘

查明原因&#xff1a; vim /usr/sbin/ufw 初步判断是python版本的问题。 # 查看python3软链接 ll /usr/bin/python3 将python3的软链接从python3.6换成之前的3.10&#xff0c;根据自己电脑情况。 可以查看下 /usr/bin 下有什么 我这是python3.10 所以解决办法是 # 移除py…

VScode远程连接主机

一、前期准备 1、Windows安装VSCode&#xff1b; 2、在VSCode中安装PHP Debug插件&#xff1b; 3、安装好Docker 4、在容器中安装Xdebug ①写一个展现phpinfo的php文件 <?php phpinfo(); ?>②在浏览器上打开该文件 ③复制所有信息丢到Xdebug: Installation instr…

百度地图 缩放 0.5 zoomend zoom_changed

设置/获取地图级别官网地址 &#xff1a; https://lbsyun.baidu.com/jsdemo.htm#bSetGetMapZoom 解决方案 //创建一个地图 let map new BMapGL.Map("container"); //这里还需定位下位置&#xff0c;这里就不详细说明了&#xff0c;官方文档有示例 //开启鼠标滚轮缩放…