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

深度学习在微纳光子学中的应用

深度学习在微纳光子学中的主要应用方向 深度学习与微纳光子学的结合主要集中在以下几个方向&#xff1a; 逆向设计 通过神经网络快速预测微纳结构的光学响应&#xff0c;替代传统耗时的数值模拟方法。例如设计超表面、光子晶体等结构。 特征提取与优化 从复杂的光学数据中自…...

LeetCode - 394. 字符串解码

题目 394. 字符串解码 - 力扣&#xff08;LeetCode&#xff09; 思路 使用两个栈&#xff1a;一个存储重复次数&#xff0c;一个存储字符串 遍历输入字符串&#xff1a; 数字处理&#xff1a;遇到数字时&#xff0c;累积计算重复次数左括号处理&#xff1a;保存当前状态&a…...

Ascend NPU上适配Step-Audio模型

1 概述 1.1 简述 Step-Audio 是业界首个集语音理解与生成控制一体化的产品级开源实时语音对话系统&#xff0c;支持多语言对话&#xff08;如 中文&#xff0c;英文&#xff0c;日语&#xff09;&#xff0c;语音情感&#xff08;如 开心&#xff0c;悲伤&#xff09;&#x…...

前端开发面试题总结-JavaScript篇(一)

文章目录 JavaScript高频问答一、作用域与闭包1.什么是闭包&#xff08;Closure&#xff09;&#xff1f;闭包有什么应用场景和潜在问题&#xff1f;2.解释 JavaScript 的作用域链&#xff08;Scope Chain&#xff09; 二、原型与继承3.原型链是什么&#xff1f;如何实现继承&a…...

网络编程(UDP编程)

思维导图 UDP基础编程&#xff08;单播&#xff09; 1.流程图 服务器&#xff1a;短信的接收方 创建套接字 (socket)-----------------------------------------》有手机指定网络信息-----------------------------------------------》有号码绑定套接字 (bind)--------------…...

USB Over IP专用硬件的5个特点

USB over IP技术通过将USB协议数据封装在标准TCP/IP网络数据包中&#xff0c;从根本上改变了USB连接。这允许客户端通过局域网或广域网远程访问和控制物理连接到服务器的USB设备&#xff08;如专用硬件设备&#xff09;&#xff0c;从而消除了直接物理连接的需要。USB over IP的…...

Angular微前端架构:Module Federation + ngx-build-plus (Webpack)

以下是一个完整的 Angular 微前端示例&#xff0c;其中使用的是 Module Federation 和 npx-build-plus 实现了主应用&#xff08;Shell&#xff09;与子应用&#xff08;Remote&#xff09;的集成。 &#x1f6e0;️ 项目结构 angular-mf/ ├── shell-app/ # 主应用&…...

08. C#入门系列【类的基本概念】:开启编程世界的奇妙冒险

C#入门系列【类的基本概念】&#xff1a;开启编程世界的奇妙冒险 嘿&#xff0c;各位编程小白探险家&#xff01;欢迎来到 C# 的奇幻大陆&#xff01;今天咱们要深入探索这片大陆上至关重要的 “建筑”—— 类&#xff01;别害怕&#xff0c;跟着我&#xff0c;保准让你轻松搞…...

Golang——7、包与接口详解

包与接口详解 1、Golang包详解1.1、Golang中包的定义和介绍1.2、Golang包管理工具go mod1.3、Golang中自定义包1.4、Golang中使用第三包1.5、init函数 2、接口详解2.1、接口的定义2.2、空接口2.3、类型断言2.4、结构体值接收者和指针接收者实现接口的区别2.5、一个结构体实现多…...

Kubernetes 节点自动伸缩(Cluster Autoscaler)原理与实践

在 Kubernetes 集群中&#xff0c;如何在保障应用高可用的同时有效地管理资源&#xff0c;一直是运维人员和开发者关注的重点。随着微服务架构的普及&#xff0c;集群内各个服务的负载波动日趋明显&#xff0c;传统的手动扩缩容方式已无法满足实时性和弹性需求。 Cluster Auto…...