【发布时间】:2017-11-02 10:39:24
【问题描述】:
我有一个咖啡应用程序,在累积 10 个咖啡点后(每杯咖啡值 2 点),您将获得一杯免费咖啡。
如何制作最多订购 10 份的咖啡,并在达到 10 点时弹出警报消息。
这是排序视图的视图控制器。没有订购咖啡时会出现错误消息,但我正在尝试在用户达到 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)
}
}
【问题讨论】:
-
需要一个变量来存储积分吗?
-
另外,发帖前请先预览一下代码格式
-
totalNumberOfCoffees 是存储点的变量@PakHoCheung