当前位置: 首页 > news >正文

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

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

 ViewController.swift:

import UIKitclass ViewController: UIViewController {var questionIndex = 0var 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 += 1nextQuestion()}func nextQuestion(){if questionIndex <= 12{questionLabel.text = queastions[questionIndex].text}else{questionIndex = 0let alert = UIAlertController(title: "漂亮!", message: "您已经完成了所有问题,要再来一遍吗?", preferredStyle: .alert)let action = UIAlertAction(title: "再来一遍", style: .default, handler: { _ inself.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 += 1scoreLabel.text = "总得分:\(score)"}else{print("huidacuowu")}}else{if queastions[questionIndex].answer == true{print("huidacuowu")}else{print("huidazhengque")score += 1scoreLabel.text = "总得分:\(score)"            }}    }override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}}

2.显示题目序号

 ViewController.swift:

import UIKitclass ViewController: UIViewController {var questionIndex = 0var 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 += 1nextQuestion()progressLable.text = "\(questionIndex + 1) / 13"}func nextQuestion(){if questionIndex <= 12{questionLabel.text = queastions[questionIndex].text}else{questionIndex = 0let alert = UIAlertController(title: "漂亮!", message: "您已经完成了所有问题,要再来一遍吗?", preferredStyle: .alert)let action = UIAlertAction(title: "再来一遍", style: .default, handler: { _ inself.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 += 1scoreLabel.text = "总得分:\(score)"}else{print("huidacuowu")}}else{if queastions[questionIndex].answer == true{print("huidacuowu")}else{print("huidazhengque")score += 1scoreLabel.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 UIKitclass ViewController: UIViewController {var questionIndex = 0var 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 += 1nextQuestion()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 = 0let alert = UIAlertController(title: "漂亮!", message: "您已经完成了所有问题,要再来一遍吗?", preferredStyle: .alert)let action = UIAlertAction(title: "再来一遍", style: .default, handler: { _ inself.questionLabel.text = queastions[0].textself.scoreLabel.text = "总得分:0"})alert.addAction(action)//present(alert, animated: true)}}func checkAnswer(_ tag: Int){if tag == 1 {if queastions[questionIndex].answer {print("huidazhengque")score += 1scoreLabel.text = "总得分:\(score)"}else{print("huidacuowu")}}else{if queastions[questionIndex].answer {print("huidacuowu")}else{print("huidazhengque")score += 1scoreLabel.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 UIKitclass ViewController: UIViewController {var questionIndex = 0var 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 += 1nextQuestion()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 = 0let alert = UIAlertController(title: "漂亮!", message: "您已经完成了所有问题,要再来一遍吗?", preferredStyle: .alert)let action = UIAlertAction(title: "再来一遍", style: .default, handler: { _ inself.questionLabel.text = queastions[0].textself.scoreLabel.text = "总得分:0"})alert.addAction(action)//present(alert, animated: true)}}func checkAnswer(_ tag: Int){if tag == 1 {if queastions[questionIndex].answer {ProgressHUD.showSucceed("答对了")score += 1scoreLabel.text = "总得分:\(score)"}else{ProgressHUD.showError("答错了")}}else{if queastions[questionIndex].answer {ProgressHUD.showError("答错了")}else{ProgressHUD.showSucceed("答对了")score += 1scoreLabel.text = "总得分:\(score)"            }}    }override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}}

 5.启动测试

相关文章:

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

1.根据用户回答计算得分 ViewController.swift: import UIKitclass ViewController: UIViewController {var questionIndex 0var score 0IBOutlet weak var questionLabel: UILabel!IBOutlet weak var scoreLabel: UILabel!override func viewDidLoad() {super.viewDidLoad()…...

AUTOSAR规范与ECU软件开发(实践篇)7.10MCAL模块配置方法及常用接口函数介绍之Base与Resource的配置

目录 1、前言 2 、Base与Resource模块 1、前言 本例程的硬件平台为MPC5744P开发板&#xff0c;主要配置MPC5744P的mcal的每个模块的配置&#xff0c;如要配置NXP的MCU之S32k324的例程请参考&#xff1a; 2 、Base与Resource模块 Base与Resource这两个模块与具体功能无关&…...

Android11编译第二弹:USB连接MTP模式+USB调试+USB信任

一、为什么需要开启USB信任和ADB调试 问题1&#xff1a;原始的AOSP&#xff0c;如果通过USB连接设备以后&#xff0c;会弹窗提示用户选择连接模式&#xff1a;MTP&#xff0c;大容量磁盘&#xff0c;照片等模式&#xff1b; 问题2&#xff1a;USB连接设备以后&#xff0c;需要…...

Unity ShaderGraph教程——基础shader

1.基本贴图shader&#xff1a; 基础贴图实现&#xff1a;主贴图、自发光贴图、光滑度贴图、自发光贴图&#xff08;自发光还加入了颜色影响和按 钮开关&#xff09;. 步骤&#xff1a;最左侧操作组——新建texture2D——新建sample texture 2D承…...

第 3 章 栈和队列(单链队列)

1. 背景说明 队列(queue)是一种先进先出(first in first out,缩为 FIFO)的线性表。它只允许在表的一端进行插入&#xff0c;而在另一端删除元素。 2. 示例代码 1&#xff09;status.h /* DataStructure 预定义常量和类型头文件 */#ifndef STATUS_H #define STATUS_H/* 函数结果…...

【DFS】1254. 统计封闭岛屿的数目

1254. 统计封闭岛屿的数目 解题思路 封闭岛屿就是上下左右全部被1包围的0 也就是靠边的0不算做封闭岛屿首先将上下左右的边界上的岛屿全部变成海洋然后在对剩下的岛屿进行DFS遍历 class Solution {public int closedIsland(int[][] grid) {// 封闭岛屿就是上下左右全部被1包…...

C#--sugarClient使用之ColumnName

使用Sugar ORM框架可以很方便地实现表名和实体名的映射&#xff0c;可以按照以下步骤进行操作&#xff1a; 创建一个实体类&#xff0c;定义实体的属性及其他信息。 [SugarTable("user_info")] // 指定实体对应的表名 public class User {public int Id { get; set…...

深度学习-4-二维目标检测-YOLOv5源码测试与训练

本文采用的YOLOv5源码是ultralytics发行版3.1 YOLOv5源码测试与训练 1.Anaconda环境配置 1.1安装Anaconda Anaconda 是一个用于科学计算的 Python 发行版&#xff0c;支持 Linux, Mac, Windows, 包含了众多流行的科学计算、数据分析的 Python 包。 官方网址下载安装包&…...

找不到msvcp140.dll的解决方法【msvcp140.dll修复工具下载】

今天&#xff0c;我将为大家分享一个与我们日常工作息息相关的话题——msvcp140.dll重新安装的5种解决方法。在接下来的时间里&#xff0c;我将向大家介绍什么是msvcp140.dll,为什么会丢失&#xff0c;以及它的用途。最后&#xff0c;我将为大家提供5种解决方法&#xff0c;帮助…...

内网隧道代理技术(二十)之 CS使用HTTP代理上线不出网机器

CS使用HTTP代理上线不出网机器 CS工具自带上线不出网机器 如图A区域存在一台中转机器,这台机器可以出网,这种是最常见的情况。我们在渗透测试的过程中经常是拿下一台边缘机器,其有多块网卡,边缘机器可以访问内网机器,内网机器都不出网。这种情况下拿这个边缘机器做中转,…...

安卓 tcp 客户端

安卓 tcp 客户端 Server:8888 是Qt 写的Tcp 服务器 ip 是 192.168.2.103 port是8888 安卓手机运行 kotlin 语法的Tcp Client &#xff0c;连接&#xff0c;收发数据 效果如下图 Tcpclient package com.example.myapplicationimport android.os.Handler import android.os.Loo…...

flutter plugins插件【三】【Flutter Intl】

3、 Flutter Intl 多语言国际化 在Android Studio中菜单Tools找到flutter intl创建多语言配置。 创建后会在pubspec.yaml出现 flutter_intl:enabled: true 在工程的lib会生成l10n与generated文件夹 l10n包含 intl_en.arb intl_zn.arb 我们在intl_en.arb添加 { home: &quo…...

简单了解ICMP协议

目录 一、什么是ICMP协议&#xff1f; 二、ICMP如何工作&#xff1f; 三、ICMP报文格式 四、ICMP的作用 五、ICMP的典型应用 5.1 Ping程序 5.2 Tracert(Traceroute)路径追踪程序 一、什么是ICMP协议&#xff1f; ICMP因特网控制报文协议是一个差错报告机制&#xff0c;…...

MVCC究竟是什么?

&#xff11;.MVCC概念 MVCC&#xff0c;全称多版本并发控制 MVCC究竟是什么&#xff1f; 通俗的来说MVCC就是为了在读取数据时不加锁来提高读取效率的一种办法&#xff0c;MVCC解决的是读写时线程安全问题&#xff0c;线程不用去抢占读写锁。MVCC中的读就是快照读&#xff0c…...

Kafka知识点总结

常见名词 生产者和消费者 同一个消费组下的消费者订阅同一个topic时&#xff0c;只能有一个消费者收到消息 要想让订阅同一个topic的消费者都能收到信息&#xff0c;需将它们放到不同的组中 分区机制 启动方法 生成者和消费者监听客户端...

K8s最基本概念

1.K8s概述和特性 k8s是谷歌在2014年开业的容器化集群管理系统 使用K8s进行容器化应用部署 使用K8s利用应用扩展 k8s目标实施让部署容器化应用更加简洁高效-------集群管理系统 1.1 K8s特性 1) 自动装箱&#xff1a;基于容器对应用运行环境的资源配置 2&#xff09;自…...

vulnhub渗透测试靶场练习2

靶场介绍 靶场名&#xff1a;easy_cloudantivirus 靶场地址&#xff1a;https://www.vulnhub.com/entry/boredhackerblog-cloud-av,453 环境搭建 依旧使用VM VirtualBox搭建靶场&#xff0c;攻击机使用的是VMware中的kali&#xff0c;需要将VMware虚拟机kali和virtualbox靶机…...

在R中安装TensorFlow、TensorFlow_Probability、numpy(R与Python系列第二篇)

目录 前言&#xff1a; 1-安装tensorflow库 Step1: 下载R包tensorflow Step2&#xff1a;安装TensorFlow库 Step3&#xff1a;导入R中 2-安装tensorflow_probability库 Step1&#xff1a;下载R包&#xff1a;tfprobability Step2&#xff1a;安装TensorFlow Probability …...

十大管理——项目成本管理

目录 1.成本管理概念 2.成本管理的四个过程域 2.1四个过程的整体理解 ​2.2四个过程的ITO口诀版记忆 2.3过程1——制定项目管理计划 ​2.4过程2——项目成本估算​ 2.5过程3——项目成本预算 2.5过程4——项目成本控制 3计算题 1.成本管理概念 项目成本管理就是要确保…...

Java BIO、NIO、AIO学习总结

前言&#xff1a;关于BIO/NIO/AIO的文章已经汗牛充栋&#xff0c;俺最近比较闲试图系统学习一下&#xff0c;希望大侠多多指教&#xff01; 先来个例子理解一下概念&#xff0c;以银行取款为例&#xff1a; 同步 &#xff1a; 自己亲自出马持银行卡到银行取钱&#xff08;使用…...

国防科技大学计算机基础课程笔记02信息编码

1.机内码和国标码 国标码就是我们非常熟悉的这个GB2312,但是因为都是16进制&#xff0c;因此这个了16进制的数据既可以翻译成为这个机器码&#xff0c;也可以翻译成为这个国标码&#xff0c;所以这个时候很容易会出现这个歧义的情况&#xff1b; 因此&#xff0c;我们的这个国…...

Opencv中的addweighted函数

一.addweighted函数作用 addweighted&#xff08;&#xff09;是OpenCV库中用于图像处理的函数&#xff0c;主要功能是将两个输入图像&#xff08;尺寸和类型相同&#xff09;按照指定的权重进行加权叠加&#xff08;图像融合&#xff09;&#xff0c;并添加一个标量值&#x…...

【论文阅读28】-CNN-BiLSTM-Attention-(2024)

本文把滑坡位移序列拆开、筛优质因子&#xff0c;再用 CNN-BiLSTM-Attention 来动态预测每个子序列&#xff0c;最后重构出总位移&#xff0c;预测效果超越传统模型。 文章目录 1 引言2 方法2.1 位移时间序列加性模型2.2 变分模态分解 (VMD) 具体步骤2.3.1 样本熵&#xff08;S…...

九天毕昇深度学习平台 | 如何安装库?

pip install 库名 -i https://pypi.tuna.tsinghua.edu.cn/simple --user 举个例子&#xff1a; 报错 ModuleNotFoundError: No module named torch 那么我需要安装 torch pip install torch -i https://pypi.tuna.tsinghua.edu.cn/simple --user pip install 库名&#x…...

保姆级教程:在无网络无显卡的Windows电脑的vscode本地部署deepseek

文章目录 1 前言2 部署流程2.1 准备工作2.2 Ollama2.2.1 使用有网络的电脑下载Ollama2.2.2 安装Ollama&#xff08;有网络的电脑&#xff09;2.2.3 安装Ollama&#xff08;无网络的电脑&#xff09;2.2.4 安装验证2.2.5 修改大模型安装位置2.2.6 下载Deepseek模型 2.3 将deepse…...

C#学习第29天:表达式树(Expression Trees)

目录 什么是表达式树&#xff1f; 核心概念 1.表达式树的构建 2. 表达式树与Lambda表达式 3.解析和访问表达式树 4.动态条件查询 表达式树的优势 1.动态构建查询 2.LINQ 提供程序支持&#xff1a; 3.性能优化 4.元数据处理 5.代码转换和重写 适用场景 代码复杂性…...

在鸿蒙HarmonyOS 5中使用DevEco Studio实现企业微信功能

1. 开发环境准备 ​​安装DevEco Studio 3.1​​&#xff1a; 从华为开发者官网下载最新版DevEco Studio安装HarmonyOS 5.0 SDK ​​项目配置​​&#xff1a; // module.json5 {"module": {"requestPermissions": [{"name": "ohos.permis…...

WPF八大法则:告别模态窗口卡顿

⚙️ 核心问题&#xff1a;阻塞式模态窗口的缺陷 原始代码中ShowDialog()会阻塞UI线程&#xff0c;导致后续逻辑无法执行&#xff1a; var result modalWindow.ShowDialog(); // 线程阻塞 ProcessResult(result); // 必须等待窗口关闭根本问题&#xff1a…...

DiscuzX3.5发帖json api

参考文章&#xff1a;PHP实现独立Discuz站外发帖(直连操作数据库)_discuz 发帖api-CSDN博客 简单改造了一下&#xff0c;适配我自己的需求 有一个站点存在多个采集站&#xff0c;我想通过主站拿标题&#xff0c;采集站拿内容 使用到的sql如下 CREATE TABLE pre_forum_post_…...

Java详解LeetCode 热题 100(26):LeetCode 142. 环形链表 II(Linked List Cycle II)详解

文章目录 1. 题目描述1.1 链表节点定义 2. 理解题目2.1 问题可视化2.2 核心挑战 3. 解法一&#xff1a;HashSet 标记访问法3.1 算法思路3.2 Java代码实现3.3 详细执行过程演示3.4 执行结果示例3.5 复杂度分析3.6 优缺点分析 4. 解法二&#xff1a;Floyd 快慢指针法&#xff08;…...