簡體   English   中英

視圖控制器被解雇后如何顯示警報

[英]How to present an alert after view controller has been dismissed

我有一個應用程序,用戶在后台上傳文件通常需要幾秒鍾。 當他們點擊“完成”按鈕時,上傳開始,這也將關閉視圖控制器。 我希望發生的是下載完成時出現警報。 我以為我只是將下面的代碼添加到上載功能中,但它不起作用。 如何顯示一個警報框,以確認上傳成功?

@IBAction func tapDone(_ sender: Any) {
    self.dismiss(animated: true, completion: nil)
    if let image = newImage {
        submit2Parse(image: image)
    }
}

func submit2Parse (image: UIImage) {
    if let pfImage = image2PFFile(image: image) {
        // Insert PFFile into parse server
        let submittedImage = PFObject(className: "Images")
        submittedImage["imageFile"] = pfImage
        submittedImage["type"] = "submittedFromUserHome"
        submittedImage["bride"] = brideSwitch.isOn
        submittedImage["groom"] = groomSwitch.isOn
        submittedImage["user"] = userSwitch.isOn
        submittedImage["picturePeriod"] = pickerSelected
        submittedImage["uploadedByUserId"] = PFUser.current()?.objectId ?? ""            submittedImage["uploadedByUserName"] = PFUser.current()?.username ?? ""
        if !txtIsPlaceHolder { submittedImage["description"] = imageDescription.text }

        submittedImage.saveInBackground { (success, error) in
            if success {
                let message = "Save in bg worked"
                print(message)
                let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
                alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: {
                    (action) in
                    self.dismiss(animated: true, completion: nil)
                }))
                self.present(alert,animated: true, completion: nil)

            } else {
                print(error?.localizedDescription ?? "")
            }
        }
    }
}

代碼給了我這個構建錯誤:

封閉中隱式使用“自我”; 使用“自我”。 使捕獲語義明確

tapDone方法中,您需要利用控制器tapDonecompletion ,如下所示:

self.dismiss(animated: true) {
    if let image = newImage {
        self.submit2Parse(image: image)
    }
}

這僅意味着self.submit2Parse(image: image)僅在關閉控制器后執行。 此外,錯誤

封閉中隱式使用“自我”; 使用“自我”。 使捕獲語義明確

僅表示您需要使用self. 在閉包內部時顯式調用變量或方法。 因此,您的submit2Parse...現在將如下所示:

self.submit2Parse(image: image)

如果我理解正確你的問題,你要解雇你SecondViewController當用戶水龍頭tapDone按鈕。 之后,一旦您的圖片上傳完成,您就希望對成功進行提醒。

但是,如果您在單擊按鈕后將其dismiss則您將不會收到任何警報,因為您的SecondViewController不再處於window hierarchy並且如果嘗試顯示警報,則會在Console中收到錯誤消息,例如:

嘗試呈現不在窗口層次結構中的視圖!

要解決此問題,您需要從您的第一個視圖控制器發出警報,該警報將在您關閉第二個視圖控制器后出現,並且可以使用delegate實現它。

考慮以下示例:

首先在FirstViewController外部聲明一個protocol

protocol UplaodSuccessDelegate:class {
    func uploadSuccess(message: String)
}

然后確認您的代表參加同一個班級:

class ViewController: FirstViewController, UplaodSuccessDelegate {

那么當您顯示SecondViewController時,您需要傳遞委托:

let vc = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
vc.delegate = self. //Pass delegate
self.present(vc, animated: true, completion: nil)

並將委托方法添加到同一類:

func uploadSuccess(message: String) {

    let message = "Save in bg worked"
    print(message)
    let alert = UIAlertController(title: "title", message: message, preferredStyle: UIAlertController.Style.alert)
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: {
        (action) in

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

現在在您的SecondViewController您需要添加

weak var delegate: UplaodSuccessDelegate?

在您的tapDone方法中,將代碼替換為:

self.dismiss(animated: true) {
    if let image = newImage {
        submit2Parse(image: image)
    }
}

上傳完成后,您需要調用委托方法(一旦上傳完成),如下所示:

self.delegate?.uploadSuccess(message: "your message")

這將從FirstViewController調用您的委托方法。

當您將下面的函數放到viewController中以在后台啟動數據上傳,然后在提交完成時觸發的閉包中調用此函數時,即使認為初始視圖控制器已被關閉(或已取消,也可以成功創建警報)如果上傳時間較長,則將其撤消)。

// This is the function that performs my background upload
    func submit2Parse (image: UIImage) {
        if let pfImage = image2PFFile(image: image) {
            // Insert PFFile into parse server
            let submittedImage = PFObject(className: "Images")
            submittedImage["imageFile"] = pfImage
            submittedImage["imageCreateDt"] = newImageCreateDate
            submittedImage["type"] = "submittedFromUserHome"
            submittedImage["bride"] = brideSwitch.isOn
            submittedImage["groom"] = groomSwitch.isOn
            submittedImage["user"] = userSwitch.isOn
            submittedImage["picturePeriod"] = pickerSelected
            submittedImage["uploadedByUserId"] = PFUser.current()?.objectId ?? ""
            submittedImage["uploadedByUserName"] = PFUser.current()?.username ?? ""
            if !txtIsPlaceHolder { submittedImage["description"] = imageDescription.text }
            // Get the image timestamp, every photo has one
            // How do you get the thumbnail image too?

            submittedImage.saveInBackground { (success, error) in
                if success {
                    let message = "Save in bg worked"
                    print(message)
                    self.showAlertFromAppDelegates()
                } else {
                    print(error?.localizedDescription ?? "")
                }
            }
        }
    }

// This is the function that creates the alert
    func showAlertFromAppDelegates(){
        var topWindow: UIWindow? = UIWindow(frame: UIScreen.main.bounds)
        topWindow?.rootViewController = UIViewController()
        topWindow?.windowLevel = UIWindow.Level.alert + 1
        let alert: UIAlertController =  UIAlertController(title: "Upload Complete", message: "Your image was successfully submitted!", preferredStyle: .alert)
        alert.addAction(UIAlertAction.init(title: "OK", style: .default, handler: { (alertAction) in
            topWindow?.isHidden = true
            topWindow = nil
        }))
        topWindow?.makeKeyAndVisible()
        topWindow?.rootViewController?.present(alert, animated: true, completion:nil)
    }

暫無
暫無

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

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