繁体   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