简体   繁体   中英

Modal pop up in swift 4

I have been searching the internet lately for an implementation of the modal dialog box for iOS programming.

I am aware of the UIAlertViewController. In fact, I am currently using this implementation in an application. But, I need something that is modal. I need the code to stop execution until the user clicks on the a button within the Alert.

So far, I haven't really seen any implementations that I am happy with. THis is the current state of my code:

func messageBox(messageTitle: String, messageAlert: String, messageBoxStyle: UIAlertControllerStyle, alertActionStyle: UIAlertActionStyle)
    {
        var okClicked = false
        let alert = UIAlertController(title: messageTitle, message: messageAlert, preferredStyle: messageBoxStyle)

        let okAction = UIAlertAction(title: "Ok", style: alertActionStyle)
        {
            (alert: UIAlertAction!) -> Void in
            okClicked = true
        }


    /*    alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: alertActionStyle, handler: { _ in
            okClicked = true
            NSLog("The \"OK\" alert occured.")
        }))*/

        alert.addAction(okAction)

        self.present(alert, animated: true, completion: nil)
        while(!okClicked)
        {

        }
    }

Should I look into creating my own dialog box hardcore style in the storyboard or does swift have some sort of implementation that I can sorta use?

If you want your own helper function but want to execute code only after the okay button has been clicked, you should look at adding a completion handler to the helper function. Here's an example:

func messageBox(messageTitle: String, messageAlert: String, messageBoxStyle: UIAlertControllerStyle, alertActionStyle: UIAlertActionStyle, completionHandler: @escaping () -> Void)
    {
        let alert = UIAlertController(title: messageTitle, message: messageAlert, preferredStyle: messageBoxStyle)

        let okAction = UIAlertAction(title: "Ok", style: alertActionStyle) { _ in
            completionHandler() // This will only get called after okay is tapped in the alert
        }

        alert.addAction(okAction)

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

I've removed unneeded code from your function and added a completion handler as the last argument. This completion handler is basically a function, and you can call it whenever you want, in this case we're calling it when the okay button is tapped. Here's how you use the function:

viewController.messageBox(messageTitle: "Hello", messageAlert: "World", messageBoxStyle: .alert) {
    print("This is printed into the console when the okay button is tapped")
}

() -> Void means "a function that takes no parameters, that returns no value", and @escaping means that the function will be called at a later date asynchronously, in this case we're calling it from the alert action's handler for when the button is tapped.

// declare an alert 
let alert = UIAlertController(title: <#Your Title#>, message:  <#Your Message#> preferredStyle: UIAlertControllerStyle.actionSheet)
//create action and add them to alert
let actionX = UIAlertAction(title:  <#Your Title#>, style: UIAlertActionStyle.default, handler: {(action) in
     // do whatever you want  
}))
alert.addAction(actionX)

You should look at closures to understand how you can code easily in swift

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM