gpt4 book ai didi

ios - 如何在按钮和警报消息上放置最大值

转载 作者:行者123 更新时间:2023-11-28 15:14:36 25 4
gpt4 key购买 nike

我有一个咖啡应用程序,在累积 10 个咖啡积分后(每杯咖啡值(value) 2 积分),您将获得一杯免费咖啡。

如何使咖啡的最大订购量为 10,并在达到 10 点时弹出警告消息。

这是排序 View 的 View Controller 。当没有订购咖啡时会出现错误消息,但我试图在用户达到 10 分时弹出另一条警报消息

import Foundation
import UIKit

class OrderingViewController: UIViewController {

var cstudent: Student = Student("name", "000")
var totalNumberOfCoffees = 0
var maximumNumberOfCoffee = 10

let minimumB = 0
let maximumA = 10

override func viewDidLoad() {
super.viewDidLoad()
}

@IBAction func cappaPressed(_ sender: Any) {
totalNumberOfCoffees += 2
}

@IBAction func flatwhitePressed(_ sender: Any) {
totalNumberOfCoffees += 2
}

@IBAction func mochaPressed(_ sender: Any) {
totalNumberOfCoffees += 2
}

@IBAction func esspresso(_ sender: Any) {
totalNumberOfCoffees += 2
}


@IBAction func placeOrderPressed(_ sender: Any) {
// by using this function, it makes the user know that they must order at least one
// coffee. if this were not here, it could waste the user's time as they
// may not be aware or may have made a mistake by not clicking on a coffee button.

if totalNumberOfCoffees > 0 { // you cannot order 0 coffees
performSegue(withIdentifier: "placeOrderSegue", sender: self)
}
displayAlertMessage(userMessage: "You cannot order no coffees!")

if minimumB >= maximumA {
totalNumberOfCoffees = maximumNumberOfCoffee
}
displayAlertMessage(userMessage: "FREE COFFEE")
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if (segue.identifier == "placeOrderSegue"){
let datasend = segue.destination as! AccountViewController
datasend.currentstudent = cstudent
// this passes the name of the student logged in, back to the
// AccountViewController. This is useful as if it were not here, the
// name of the user would be lost.

datasend.currentstudent.numOfCoffees += totalNumberOfCoffees
// add coffees bought to current student. this makes the
// student logged in know how many coffees they're oreder.
}
}

// alert message.
func displayAlertMessage (userMessage: String) {
let myAlert = UIAlertController(title: "Nothing has been ordered", message:userMessage, preferredStyle: UIAlertControllerStyle.alert)

let okAction = UIAlertAction(title: "ok", style: UIAlertActionStyle.default, handler: nil)
//the okAction is the button which is pressed by the users which lets them re-order on
// the same view controller (orderingViewController)

myAlert.addAction(okAction)

self.present(myAlert, animated: true, completion: nil)
}

}

最佳答案

  1. 当达到 10 分时弹出警告消息。

简单的答案是创建一个 checkReach 函数来检查 totalNumberOfCoffees 是否大于或等于 10

@IBAction func cappaPressed(_ sender: Any) {
totalNumberOfCoffees += 2
checkReach()
}

@IBAction func flatwhitePressed(_ sender: Any) {
totalNumberOfCoffees += 2
checkReach()
}

@IBAction func mochaPressed(_ sender: Any) {
totalNumberOfCoffees += 2
checkReach()
}

@IBAction func esspresso(_ sender: Any) {
totalNumberOfCoffees += 2
checkReach()
}

func checkReach() {
if totalNumberOfCoffees >= 10 {
displayAlertMessage (userMessage: "REACHED")
totalNumberOfCoffees = 10
}
}

关于ios - 如何在按钮和警报消息上放置最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47073242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com