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

泛微E9开发实战:如何实现跨月份自动计算结束日期(附完整代码)

泛微E9开发实战&#xff1a;跨月份日期计算的工程化解决方案 财务报销周期自动闭合、项目里程碑智能推算、合同履约期限动态生成——这些高频业务场景背后&#xff0c;都藏着一个让泛微E9开发者头疼的日期计算难题。当开始日期遇上月末临界点&#xff0c;简单的天数相加就会引发…...

别再为小程序合法域名发愁了!手把手教你用宝塔+FRP搞定内网穿透与HTTPS配置

微信小程序合法域名配置实战&#xff1a;从内网穿透到HTTPS全流程指南 当你兴致勃勃地开发完微信小程序的后端接口&#xff0c;准备在真机测试时&#xff0c;却遭遇"不在合法域名列表中"的报错——这种挫败感我深有体会。三年前我的第一个小程序项目就卡在这个环节整…...

打破音乐枷锁:ncmdumpGUI让你的NCM文件重获自由

打破音乐枷锁&#xff1a;ncmdumpGUI让你的NCM文件重获自由 【免费下载链接】ncmdumpGUI C#版本网易云音乐ncm文件格式转换&#xff0c;Windows图形界面版本 项目地址: https://gitcode.com/gh_mirrors/nc/ncmdumpGUI 你下载的音乐其实并不属于你。当你在网易云音乐客户…...

用Cursor+LocalStorage实现无后端项目管理:前端开发者的轻量级解决方案

用CursorLocalStorage实现无后端项目管理&#xff1a;前端开发者的轻量级解决方案 在当今快节奏的开发环境中&#xff0c;前端开发者常常需要快速搭建小型项目管理工具来跟踪个人或团队的工作进度。传统方案往往需要配置数据库、搭建后端API&#xff0c;这对于简单需求来说显得…...

低门槛AI视频生成新选择:opensora-hpcai本地部署与优化指南

低门槛AI视频生成新选择&#xff1a;opensora-hpcai本地部署与优化指南 【免费下载链接】opensora-hpcai-1_0_ms MindSpore implementation of OpenSora, an open-source project that aims to foster innovation, creativity, and inclusivity within the field of content cr…...

Ollama安装路径优化:从C盘迁移到D盘的完整指南

1. 为什么需要迁移Ollama到D盘&#xff1f; 很多AI开发者在Windows系统上初次安装Ollama时&#xff0c;都会遇到一个头疼的问题——默认安装路径在C盘。随着模型文件的不断下载和项目积累&#xff0c;C盘空间很快就会被占满。我自己就经历过C盘爆红的尴尬&#xff0c;系统卡顿不…...

轴,V带轮,斜齿轮,丝杠零件图CAD图纸

轴作为机械系统中的核心传动部件&#xff0c;承担着传递扭矩与支撑旋转的重要功能。其设计需综合考虑材料强度、刚度及热处理工艺&#xff0c;以确保在复杂载荷下保持稳定运行。典型结构包含阶梯轴、空心轴等类型&#xff0c;通过优化轴肩定位与键槽布局&#xff0c;可有效提升…...

FanControl实战指南:从噪音困扰到智能散热的转型之路

FanControl实战指南&#xff1a;从噪音困扰到智能散热的转型之路 【免费下载链接】FanControl.Releases This is the release repository for Fan Control, a highly customizable fan controlling software for Windows. 项目地址: https://gitcode.com/GitHub_Trending/fa/…...

【分箱基础篇】pandas 分箱双子星:pd.cut 与 pd.qcut

进阶篇参考&#xff1a;【分箱进阶篇】分箱的工程细节&#xff1a;从训练到部署的完整模式 拿到一列连续数值&#xff1a;年龄、收入、交易金额等&#xff0c;第一步常常是分箱&#xff0c;也就是把连续值映射到几个离散区间。pandas 提供了两个内置函数干这件事&#xff1a;pd…...

5步打造高效知识管理系统:Obsidian模板库实战指南

5步打造高效知识管理系统&#xff1a;Obsidian模板库实战指南 【免费下载链接】OB_Template OB_Templates is a Obsidian reference for note templates focused on new users of the application using only core plugins. 项目地址: https://gitcode.com/gh_mirrors/ob/OB_…...