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

时间:2017-11-02 10:39:24

标签: ios swift button

我有一个咖啡应用程序,累积了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)
    }

}

1 个答案:

答案 0 :(得分:1)

  1. 当达到10分时弹出警告信息。
  2. 简单的答案是创建一个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
        }
    }