How to create alerts in Xcode


You may want to give a warning or additional messages with alerts that let users know their misbehavior in the application. These alerts can be presented using UIAlerts a pop-up that comes in the ios applications. You can also display messages in alert views. In addition to messages, you can link activities to your alert to provide a way for the user to react.

Let's create a small app that displays an alert when a button is tapped.

Step 1: Create a new Xcode Project

Navigate to Xcode Application -> Select create a new Xcode project ->Select iOS on the top, then select single view app and click on next-> Give a Name to your application -> Provide the path to save the application and then click on create

create a new iOS project

Step 2: Add Button

Navigate to main.storyboard press ( command + shift + L ) select button and drag it to the View Controller.

Right-click on the button and press control and drag it to viewcontroller.swift select action and click on connect.

Add button

Step 3: Initiate Alert

Write UIAlertController() function in @IBAction func ButtonTapped

UIAlertController() function invokes alert view when the button is tapped you can provide an alert title, message in the arguments which will be displayed in the alert.

In addition, you can also provide actions that can be performed using addAction() function.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akshay Devkate

Tech enthusiast i write blogs on Web Development and Mobile app development.