Display Alert Box In Swift

In this article, we will create an Alert Box with an OK Button. Also, in the end, I’ll provide the code to create an alert box with OK and Cancel buttons. So, let’s get started.
  1. Open XCode and create a New XCode Project.

    Display Alert Box In Swift
  1. Select iOS-> Single View App and Click Next.

    Display Alert Box In Swift
  1. Name your project and select the language as Swift. Click Next. choose a desired location to save your project and click Create.

  2. On the left panel, you can see all the files associated with your project. Open Main.storyboard. Here, we will create our UI.

  3. In the Object Library, you will get all the tools to design your UI. Just type the tool name you need at the bottom, and when it appears in the Object Library, drag and drop it in your View Controller.

  4. I have created a button in my UI. I want an alert message to be displayed on the button-click.

    Display Alert Box In Swift
  1. You can change the Background color, Text color, Font size, etc as per your requirements in the Attributes Inspector. You can also rename your tool by double-clicking on it.

  2. For the next step, you need to open the Assistant Editor. On the right-hand side of your screen, on the top, you may see an icon with two circles overlapping each other. That’s your Assistant Editor. On clicking on it, you will be able to open the View Controller of your project.

  3. To connect your button to the View Controller, drag your mouse from your Button towards the View Controller along with the ‘Ctrl’ button pressed. You will see a blue line while dragging your mouse from your button.

    Display Alert Box In Swift

  4. When you release your mouse, a box appears. Enter the data as shown in the below figure and then press Connect. You can give any name to your button.

    Display Alert Box In Swift

    For the Alert Box to display, paste the following code inside the Action block of your button.
    1. let alertController = UIAlertController(title: "Alert", message: "Button clicked!", preferredStyle: .alert)  
    2. let OKAction = UIAlertAction(title: "OK", style: .default) {  
    3.     (action: UIAlertAction!) in  
    4.     // Code in this block will trigger when OK button tapped.  
    5.     print("Ok button tapped");  
    6. }  
    7. alertController.addAction(OKAction)  
    8. self.present(alertController, animated: true, completion: nil)  
  1. Now we are ready to run our project. Run your project in the simulator/iPhone device. On clicking on the button, an alert box must display in the similar manner.

    Display Alert Box In Swift

    Code to display Alert Box with OK and Cancel Buttons,
    1. let alertController = UIAlertController(title: "Alert title", message: "Message to display", preferredStyle: .alert)  
    2. // Create OK button  
    3. let OKAction = UIAlertAction(title: "OK", style: .default) {  
    4.     (action: UIAlertAction!) in  
    5.     // Code in this block will trigger when OK button tapped.  
    6.     print("Ok button tapped");  
    7. }  
    8. alertController.addAction(OKAction)  
    9. // Create Cancel button  
    10. let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) {  
    11.     (action: UIAlertAction!) in print("Cancel button tapped");  
    12. }  
    13. alertController.addAction(cancelAction)  
    14. // Present Dialog message  
    15. self.present(alertController, animated: true, completion: nil)  

Similar Articles