当前位置: 首页 > 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;使用…...

【佛山大学主办,土木与交通学院承办 | 施普林格Springer系列出版 | EI、Scopus检索 | 另期刊论文征稿】第九届结构工程与工业建筑国际学术会议(ICSEIA 2026)

第九届结构工程与工业建筑国际学术会议&#xff08;ICSEIA 2026&#xff09; 2026 9th International Conference on Structural Engineering and Industrial Architecture 2026年7月3-5日 中国佛山 大会官网&#xff1a;www.icseia.com【论文投稿】 截稿时间&#xff1a;…...

终极指南:SSDD数据集在SAR舰船检测中的完整应用方案

终极指南&#xff1a;SSDD数据集在SAR舰船检测中的完整应用方案 【免费下载链接】Official-SSDD SAR Ship Detection Dataset (SSDD): Official Release and Comprehensive Data Analysis 项目地址: https://gitcode.com/gh_mirrors/of/Official-SSDD SSDD&#xff08;S…...

DB2数据字段拼接实战:从LISTAGG到XMLAGG的进阶应用与避坑指南

1. 从LISTAGG到XMLAGG&#xff1a;DB2字段拼接的进化之路 第一次在DB2里用LISTAGG函数时&#xff0c;那种丝滑的体验让我误以为找到了终极解决方案——直到某天凌晨三点&#xff0c;生产环境突然报警&#xff0c;日志里赫然写着"SQL0407N The result of a scalar fullsel…...

告别时序警告!手把手教你为Vivado自定义分频器添加正确时钟约束

深度解析Vivado分频器时钟约束&#xff1a;从原理到实战的全链路指南 在FPGA开发中&#xff0c;时钟管理是确保设计稳定性的核心环节。当我们面对低频应用场景时&#xff0c;常常需要将高频系统时钟分频至工作频率&#xff0c;而Vivado工具链对这类自定义分频器的时序约束有着特…...

猫头鹰的秘密网络

原文&#xff1a;towardsdatascience.com/the-secret-network-of-owls-d55e7b2c4910 你知道 8 月 4 日是国际猫头鹰意识日吗&#xff1f;我也不知道&#xff0c;直到无聊地浏览可爱的猫头鹰表情包&#xff0c;这让我来到了这个网站。然后&#xff0c;正如我们最近在我们的花园里…...

终极AMD Ryzen调试工具SMUDebugTool:免费开源的硬件掌控神器

终极AMD Ryzen调试工具SMUDebugTool&#xff1a;免费开源的硬件掌控神器 【免费下载链接】SMUDebugTool A dedicated tool to help write/read various parameters of Ryzen-based systems, such as manual overclock, SMU, PCI, CPUID, MSR and Power Table. 项目地址: http…...

告别卡顿!用Sunshine打造私人游戏串流服务器的完整指南

告别卡顿&#xff01;用Sunshine打造私人游戏串流服务器的完整指南 【免费下载链接】Sunshine Self-hosted game stream host for Moonlight. 项目地址: https://gitcode.com/GitHub_Trending/su/Sunshine 你是否曾经梦想过在任何设备上流畅玩PC游戏&#xff1f;无论是躺…...

脉冲神经网络:低功耗AI计算的生物启发革命

1. 脉冲神经网络&#xff1a;生物启发的低功耗计算革命2014年&#xff0c;IBM发布TrueNorth芯片时&#xff0c;其每平方厘米功耗仅20毫瓦的性能震惊了整个AI界。这款基于脉冲神经网络(SNN)的芯片&#xff0c;能耗仅为传统CPU的万分之一&#xff0c;却能够实时处理视频流中的复杂…...

Vue 项目中 vis-network 点击节点不生效的问题排查:外层 transform 缩放导致坐标偏移

最近在 Vue3 项目中把一个原生 HTML 版知识图谱迁移成 Vue 组件。原始 HTML 使用 vis-network 实现节点关系图&#xff0c;点击节点可以正常展开下一级。但迁移到 Vue 项目后&#xff0c;图谱可以正常渲染&#xff0c;节点、连线、布局都没有问题&#xff0c;唯独点击节点时无法…...

别再手动算考勤了!我用Python+企业微信API写了个自动统计脚本(附源码)

告别手工考勤&#xff1a;Python企业微信API自动化统计实战指南 每次月底统计考勤时&#xff0c;行政同事总要加班到深夜&#xff0c;手动核对上百条打卡记录。迟到、早退、外勤打卡...各种状态让人眼花缭乱。作为技术团队的一员&#xff0c;我决定用Python企业微信API打造一个…...