Learn iOS UI alerts in simple language
In this section, we will learn an example of UI alerts in iOS apps; Also with this example we will learn how to transfer important information using Xcode in swift.
IOS UI alerts
iOS UI alerts, In iOS apps; Alerts are used to alert the user rather than inform the user; Actions that the user must take; Do not procrastinate. In general, iOS uses these alerts to alert users to low battery levels so that they can stop their activities on the device; Connect their device to the charger.
In iOS apps, alerts for each app; After opening; The screen appears as follows, so that the user can continue using the program before; Must manually close the displayed warning message.
IOS Alert; as shown above; The alert title includes a text message and a button or suitcase; And we have no other choice to change the alert style.
We can use these alerts in our iOS apps by adding the UIAlertsView class reference.
Here’s an example of how UI alerts can be applied to iOS apps.
Create an iOS alert app in Swift
To create a new project in Xcode on iOS, open Xcode from the application folder list. After opening Xcode, the welcome window will open as shown below. In the Welcome window, click on the second option, “Create a new Xcode Project”; Click (create a new Xcode Project) or select the File New Project path.
After selecting “Create a new Xcode project”, a new window will open in which we must select our template.
The new Xcode window includes several built-in application templates to implement the usual type of iOS apps, such as page-based apps, tab-based apps, games, spreadsheet apps, and more. These templates have a preset interface and source code files. .
For example, UI alerts in iOS, we have the most basic pattern of the program, which is “demo only”; We will use. To select this item, go to the iOS section on the left, select the application from the select section, and in the main part of the window that opens, select “single view application” and click on the next button, as shown below. .
After clicking Next, we will see a window like the one below, in this case we have to mention the project name and other details for our program.
Product name: “Alerts in iOS” (Alerts in iOS)
The name we enter in the Product Name section is used for the project and application.
Organization name: “Tutlane”
You can enter the name of the organization or your name in this field; Of course you can leave that section blank.
Organization Identifier): “com.developersocociety)”
If you do not have an enterprise ID, enter com.example.
Bundle Identifier: This section is automatically generated based on the terms we entered in the product name and organization ID.
Language: “Swift”
Select the language type “Swift” because we want to develop applications using swift.
Universal (devices): “Universal”
Select the Devices option as Universal This means that this app is for all Apple devices; If you need to run the app for iPad only, you can select the iPad option to restrict your app to running only on iPad devices.
Use core Data: Not selected
This option is used for database operations. Select this option if you are doing any database-related operations in your application, otherwise do not select this option.
Include Unit Test: Not selected
If you need unit tests for your application, select this option otherwise leave it unselected.
Includes UI tests: not selected
Select this option if you need UI tests for your application, otherwise do not select it.
After completing the options, click the Next button as shown below.
When we click the Next button; A new window will open which should be used to save the position in which we want the new project; Let’s choose. Once you have selected the new project storage location; As shown below; Click the Create button.
After clicking the Create button, Xcode opens and creates a new project. In our project, Main.storyboard and ViewController.swift are the main files used to design the user interface and maintain the source code.
Main.storyboard – which is the visual interface editor and this file is used to design the application user interface.
ViewController.swift – which contains the source code of our application and we use this file to write any code related to our application.
Now select the Main.storyboard file in the project, until Xcode opens the visual interface editor as shown below.
Now select the ViewController.swift file in your project, which looks similar to the following:
Add iOS app UI controls to the display in Swift
We are now adding controls to our application for that library of available objects. The object library appears at the bottom right of Xcode. If you did not find the object library, as shown below; Click the button that is the third button in the library selection bar on the left. (On the other hand, you can select the View Utilities Show Object Library path).
As we have suggested; Our interface is the Main.storyboard file, so open the Main.storyboard file. Now look for the button in the Object Library in the Filter box and then the following; Drag the button into the ViewController on the Main.storyboard.
Communicate UI controls to coding in Swift
We now establish the connection between the controls and the coding of ViewController.Swift. To do this, click on the Assistant button, which is located in the lower right corner of the Xcode toolbar.
To map controls; Hold down the Ctrl key on the keyboard and release the button from the interface and release the same in the ViewController.swift file below.
After completing the button mapping, write the following code in the ViewController.swift file in the button function.
@IBAction func buttonalert (sender: AnyObject) {
let alert = UIAlertController (title: “Tutlane Alert”, message: “My Alert for Tutlane”, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction (UIAlertAction (title: “Ok”, style: UIAlertActionStyle.Default, handler: nil))
alert.addAction (UIAlertAction (title: ”Cancel”, style: UIAlertActionStyle.Default, handler: {(action: UIAlertAction!) inprint (“you have pressed the Cancel button”)
}))
Self.presentViewController (alert, animated: true, completion: nil)
}
When we have written all the required functions in the ViewCntroller.swift file; Our coding should be similar to the following:
Import UIKit
Class ViewController: UIViewController {
@IBOutlet weak var alert: UIButton!
@IBAction func buttonalert (sender: AnyObject) {
let alert = UIAlertController (title: “Tutlane Alert”, message: “My Alert for Tutlane”, PreferredStyle: UIAlertControllerStyle.Alert)
alert.addAction (UIAlertAction (title: ”OK”, style: UIAlertActionStyle.Default, handler: nil))
alert.addAction (UIAlertAction (title: ”Cancel”, style: UIAlertActionStyle.Default, handler: {(action: UIAlertAction!) inprint (“you have pressed the Cancel button”)
}))
Self.presentViewController (alert, animated: true, completion: nil)
}
Override func viewDidLoad () {
Super.viewDidLoad ()
// Do any additional setup after loading the view, typically from a nib.
}
Override func didRecieveMemoryWarning () {
super.didReceiveMemoryWarning ()
// Dispose of any resources that can be recreated.
}
}
Now run the program and check its output. To implement the program; Select the required emulator (here we can choose the iPhone Six S Plus) and click the Play button; Which is similar to the one below in the upper left corner of the Xcode toolbar.
IOS alerts output in Swift language
Below you can see the result of the iOS Alerts app. If you look at it by default; You will see a button that when clicked will display a similar message below.
In this way, we can use the UI alerts in the iOS application in the Swift application to provide important information to the user and he can take the relevant actions.