Simplified iOS

  • Home
  • About
  • Contact
  • Advertise
  • Write for Us

iOS Show Alert using UIAlertController Tutorial in Swift and XCode 7

August 5, 2016 by Belal Khan 1 Comment

In this iOS Show Alert Tutorial we will see how we can pop up an alert box in our iPhone Apps. An alert dialog is very important component for every app and we frequently need it. So in this iOS Show tutorial I will be creating a very simple Alert Dialog in XCode using UIAlertController. Now lets start our tutorial.

What is an Alert Dialog?

Alert dialog is a simple view that pops up in the app to show some alerts to user. You can see a simple alert in the below image. And in this iOS Show Alert Tutorial we will be learning how we can implement the same using Xcode and Swift.

ios show alert

iOS Alert Dialog Example

UIAlertController

To display alert dialogs in swift we have this class. It is quite simple to display an alert dialog using this class. You can use the following code to achieve your goal.

        //Creating the alert controller
        //It takes the title and the alert message and prefferred style
        let alertController = UIAlertController(title: "Hello  Coders", message: "Visit www.simplifiedios.net to learn xcode", preferredStyle: .Alert)
        
        //then we create a default action for the alert... 
        //It is actually a button and we have given the button text style and handler
        //currently handler is nil as we are not specifying any handler
        let defaultAction = UIAlertAction(title: "Close Alert", style: .Default, handler: nil)
        
        //now we are adding the default action to our alertcontroller
        alertController.addAction(defaultAction)
        
        //and finally presenting our alert using this method
        presentViewController(alertController, animated: true, completion: nil)

iOS Show Alert Tutorial

Now lets learn how you can implement the same in your Xcode project. We will start by creating a new project.

Creating a new Xcode Project

  • As we did in the last tutorials create a new Xcode Project.
  • I have created AlertExample.
  • Select Single View App create your project.

I will not be posting screenshots of these common processes, as I have already done this in the last tutorials. For example you can check this UITableViewController Tutorial Swift to get the details.

Adding a Button to Our View

  • Now go to Main.storyboard and drag a Button inside.
  • Now we have a button in our main view.

ios show alert

Connecting Button to Swift Code

  • Now connect the button to your swift code. We have discussed it in Xcode Button Tutorial. It is very simple just open your assistant editor, press control and drag the button to your swift code window.
  • Then in the popup fill the values as follows and click on Connect.

connecting xcode button tutorial

  • Now you will see the following code in your ViewController.swift file.
//
//  ViewController.swift
//  AlertExample
//
//  Created by Belal Khan on 04/08/16.
//  Copyright © 2016 Belal Khan. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBAction func buttonClick(sender: UIButton) {
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

iOS Show Alert using UIAlertController

  • Now inside the function buttonClick just write the code we seen before. So the final code will be.
//
//  ViewController.swift
//  AlertExample
//
//  Created by Belal Khan on 04/08/16.
//  Copyright © 2016 Belal Khan. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBAction func buttonClick(sender: UIButton) {
        
        let alertController = UIAlertController(title: "Hello  Coders", message: "Visit www.simplifiedios.net to learn xcode", preferredStyle: .Alert)
        let defaultAction = UIAlertAction(title: "Close Alert", style: .Default, handler: nil)
        alertController.addAction(defaultAction)
        
        presentViewController(alertController, animated: true, completion: nil)

        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Executing the App in Simulator

  • Thats it now run your application in the Simulator.
ios show alert

iOS Alert Dialog Example

  • Bingo! Its working absolutely fine.

So thats all for this iOS Show Alert Tutorial friends. Feel free to leave your comments if having any confusions or doubts related to this iOS Show Alert Tutorial. And don’t forget to share this post if you found it useful. Thank You 🙂

Share this:

  • Tweet
  • Share on Tumblr
  • WhatsApp

Related

Filed Under: iOS Development Tutorial Tagged With: ios show alert, uialertcontroller swift, xcode alert dialog

About Belal Khan

I am Belal Khan, I am currently pursuing my MCA. In this blog I write tutorials and articles related to coding, app development, iphone etc.

Comments

  1. abdel says

    March 3, 2018 at 11:08 am

    just after running tha app, Xcode display two winodws of code, i m new in swift.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *




About Me

belal khan simplified ios

Hello I am Belal Khan, founder and owner of Simplified iOS. I am currently pursuing MCA from St. Xavier's College, Ranchi. Apart from my academic I am a blogger, I run various websites and majority of them are about coding and development.

Connect with Me

Follow Simplified iOS

Simplified iOS

Popular Posts

  • Swift SQLite Tutorial for Beginners – Using… (99,456)
  • Swift PHP MySQL Tutorial – Connecting iOS App… (98,346)
  • UIWebView Example to Load URL in iOS using Swift in Xcode (80,016)
  • Download Full High Sierra Installer to Create Bootable USB (70,344)
  • Xcode Login Screen Example using Swift 3, PHP and MySQL (67,312)
  • How to Format USB on Mac? Formatting External Hard… (61,391)
  • Swift JSON Tutorial – Fetching and Parsing… (59,413)
  • Firebase Realtime Database Tutorial for Swift using Xcode (53,721)
  • iOS Registration Form Example using PHP and MySQL (48,984)
  • Xcode Text Field Tutorial for iOS Application using Swift (41,225)




About

Simplified iOS is a blog where you can find latest tutorials related to coding and app development for iphone and MAC OS. Here you can get Simplified iPhone, iPad and Mac OS development tutorial to know the things about Apple Development from basics to advanced level.

Quick Links

  • Advertise
  • Contact
  • Disclaimer
  • Privacy Policy
  • Write for Us

Copyright © 2017 · Simplified iOS· All rights Reserved. And Our Sitemap.All Logos & Trademark Belongs To Their Respective Owners·