How to call my custom alert controller function to display in other view controller?

后端 未结 2 1726
不思量自难忘°
不思量自难忘° 2021-01-20 12:38

I have write my custom alert view controller.But,I am having error at calling my alert controller from other view controller.It show me the error that I described below.

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-20 12:54

    Change your method like so:

    func displayLoadingAlert(viewController: UIViewController?) -> UIAlertController {
        var controllerToPresent = viewController
        if controllerToPresent == nil {
        controllerToPresent = self
    }
    
    // Most of your code
    
    controllerToPresent.presentViewController(loadingAlertController, animated: true, completion: nil)
    
    return loadingAlertController
    }
    

    Then when you're calling the alert:

    loadingAlertController.displayLoadingAlert(self)
    

    Alternatively: Rename the method displayLoadingAlert to loadingAlert

    Remove the line:

    self.presentViewController(loadingAlertController, animated: true, completion: nil)
    

    then when calling insidethe showAlert() method

    let loadingAlertController = loadingAlertController.loadingAlert()
    self.presentViewController(loadingAlertController, animated: true, completion: nil)
    

提交回复
热议问题