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

天涯浪子 提交于 2019-12-01 20:50:05

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)

I see you made as subclass, but in the UIAlertController documentation is written:

The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

I would recommend you do not go against it. If you need something very custom better to use UIViewController and display with custom behavior.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!