0
私は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)
}
}
以上であるかどうかを確認するために必要checkReach funcを作成するだろうか? –
また、投稿する前にコードフォーマットをプレビューしてください –
totalNumberOfCoffeesはポイントを格納するvarです@PakHoCheung –