Cally Chan
Cally Chan

Reputation: 53

swift3 call alert function from other swift.file

I am new of swift3. Now, I am finding a way to call alert function from other swift.file

Like this:

//MainView.swift
//Call function
AlertFun.ShowAlert(title: "Title", message: "message..." )

//Another page for storing functions
//Function.swift

public class AlertFun {
    class func ShowAlert(title: String, message: String ) {    
        let alert = UIAlertController(title: tile, message: message, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}

Problem in here...Cannot do this in this way....

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

How can I implement it? Thanks.

Upvotes: 2

Views: 3285

Answers (3)

Martin Geary
Martin Geary

Reputation: 1

I found that none of the examples I've seen will work without getting the warning:

Attempt to present <UIAlertController: 0x7f82d8825400> on <app name> whose view is not in the window hierarchy!

The code that works for me is as follows. The function call is as before:

 AlertFun.ShowAlert(title: "Title", message: "message...", in: self)

However, to make this work, the Function.swift file has to display the alert inside the DispatchQueue.main.async. So the Function.swift file should look like this:

public class AlertFun
{
    class func ShowAlert(title: String, message: String, in vc: UIViewController)
    {
        DispatchQueue.main.async
            {
                let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
                alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil))
                vc.present(alert, animated: true, completion: nil)
        }
    }
}

Upvotes: 0

Yogendra Girase
Yogendra Girase

Reputation: 631

Call Method for your controller

Utility.showAlertOnViewController(targetVC: self, title: "", message:"")

Your Class

class Utility: NSObject {
    class func showAlertOnViewController(
            targetVC: UIViewController,
            title: String,
                message: String)
        {

            let alert = UIAlertController(
                title: title,
                message: message,
                preferredStyle: UIAlertControllerStyle.alert)
            let okButton = UIAlertAction(
                title:"OK",
                style: UIAlertActionStyle.default,
                handler:
                {
                    (alert: UIAlertAction!)  in
            })
            alert.addAction(okButton)
            targetVC.present(alert, animated: true, completion: nil)
        }
}

Upvotes: 2

rockdaswift
rockdaswift

Reputation: 9983

Pass the viewController reference as a parameter to the showAlert function like:

//MainView.swift
//Call function
AlertFun.ShowAlert(title: "Title", message: "message...", in: self)

//Another page for storing functions
//Function.swift

public class AlertFun {
    class func ShowAlert(title: String, message: String, in vc: UIViewController) {    
        let alert = UIAlertController(title: tile, message: message, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
        vc.present(alert, animated: true, completion: nil)
    }
}

Upvotes: 5

Related Questions