簡體   English   中英

迅速完成進度

[英]Implementing Completion Block swift

我實現了具有邏輯錯誤的完成塊。 我想在單擊checkOutBtn時首先觸發checkFields,以檢查所有文本字段是否都不為空,然后再觸發執行添加方式之前,它觸發addingDeliveryAddress()方法插入數據庫。 但是,當單擊checkOutBtn時,它無法正常工作,並繼續執行segueway。 感謝你的幫助。 謝謝

   @IBAction func checkOutBtn(_ sender: Any) {

    checkFields { (results) in
        if results {
            self.addingDeliveryAddress()
        }
    }
}


func checkFields(_ completion: @escaping (Bool) -> ()){
        if (recipientName.text?.isEmpty)! {
            errorMessageLbl.textColor = UIColor.red
            errorMessageLbl.text = "Enter Recipient Name"
            completion(false)
        }else if (recipientMobile.text?.isEmpty)! {
            errorMessageLbl.textColor = UIColor.red
            errorMessageLbl.text = "Enter Recipient Mobile Number"
            completion(false)
        }else if (recipientArea.text?.isEmpty)! {
            errorMessageLbl.textColor = UIColor.red
            errorMessageLbl.text = "Enter Recipient Area"
            completion(false)
        }else if (recipientAddress.text?.isEmpty)! {
            errorMessageLbl.textColor = UIColor.red
            errorMessageLbl.text = "Enter Recipient Address"
            completion(false)
        }
        completion(true)
    }



    //Adding Delivery Address
    func addingDeliveryAddress(){

        //getting user data from defaults
        let defaultValues = UserDefaults.standard
        let userId = defaultValues.string(forKey: "userid")

        //creating parameters for the post request
        let parameters: Parameters=[
            "recipientName":recipientName.text!,
            "recipientPhoneNumber":recipientMobile.text!,
            "recipientArea":recipientArea.text!,
            "recipientAddress":recipientAddress.text!,
            "nearestLandmark":recipientLandmark.text!,
            "userId":Int(userId!)!
        ]

        //Constant that holds the URL for web service
        let URL_ADD_DELIVERY_ADDRESS = "http://localhost:8888/restaurant/addDeliveryAddress.php?"

        Alamofire.request(URL_ADD_DELIVERY_ADDRESS, method: .post, parameters: parameters).responseJSON {
            response in
            //printing response
            print(response)

            let result = response.result.value

            //converting it as NSDictionary
            let jsonData = result as! NSDictionary

            //if there is no error
            if(!(jsonData.value(forKey: "error") as! Bool)){

                self.performSegue(withIdentifier: "toCheckOut", sender: self)

            }else{

                let alert = UIAlertController(title: "No Delivery Address", message: "Enter Delivery Address to continue", preferredStyle: .alert)

                alert.addAction(UIAlertAction(title: "Ok", style: .destructive, handler: nil))
                //alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))

                self.present(alert, animated: true)
            }
        }
    }

為什么要完成一個區塊? 沒有異步過程。

我建議以這種方式成功返回(直接)錯誤字符串或空字符串。

@IBAction func checkOutBtn(_ sender: Any) {

    let result = checkFields()
    if result.isEmpty {
        self.addingDeliveryAddress()
    } else {
       errorMessageLbl.textColor = UIColor.red
       errorMessageLbl.text = "Enter Recipient " + result
    }
}

func checkFields() -> String {
    if recipientName.text!.isEmpty {
        return "Name"
    } else if recipientMobile.text!.isEmpty {
        return "Mobile Number"
    } else if recipientArea.text!.isEmpty {
        return "Area"
    } else if recipientAddress.text!.isEmpty {
        return "Address"
    }
    return ""
}

在您的代碼中,您使用@escaping閉包。 這是錯誤的,因為您沒有在此閉包主體中執行任何異步操作。 當使用@escaping時 ,閉包將被保留以在以后執行,並且函數的主體將被執行。 這就是為什么在檢查任何內容之前會觸發addingDeliveryAddress() 你的閉包函數應該像這樣@nonescaping

func checkFields(_ completion: (Bool) -> ()){
        if (recipientName.text?.isEmpty)! {
            errorMessageLbl.textColor = UIColor.red
            errorMessageLbl.text = "Enter Recipient Name"
            completion(false)
        }else if (recipientMobile.text?.isEmpty)! {
            errorMessageLbl.textColor = UIColor.red
            errorMessageLbl.text = "Enter Recipient Mobile Number"
            completion(false)
        }else if (recipientArea.text?.isEmpty)! {
            errorMessageLbl.textColor = UIColor.red
            errorMessageLbl.text = "Enter Recipient Area"
            completion(false)
        }else if (recipientAddress.text?.isEmpty)! {
            errorMessageLbl.textColor = UIColor.red
            errorMessageLbl.text = "Enter Recipient Address"
            completion(false)
        }
        completion(true)
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM