Simplified iOS

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

iOS Dialog Box with Input using UIAlertController in Swift

March 10, 2017 by Belal Khan 5 Comments

In this post we will learn building an iOS dialog box with input using UIAlertController. An input dialog is many times required in the application. So lets see how we can build it for our iOS Application.

Building iOS Dialog Box with Input

Creating a new Xcode Project

  • As always we will start creating a new SingleView Application.
  • So open Xcode and create a new project.
  • Remember you have to create a SingleView Application with Swift.

Creating UI

  • Nothing much for UI in this tutorial. We only need a Label and a Button.
  • So put a Label and a Button in your Main.storyboard as shown in the below image.

iOS Dialog Box with Input

Connecting Views with Code

  • Now again we need to connect the Label as an Outlet. And the Button as an Action.
  • This is very easy just open Assistant Editor, press control and drag the view to the your class. If you don’t understand what I am saying you can go through the previous tutorials about Xcode Button Tutorial and Xcode TextField Tutorial.
  • So once you have connected your views your code will be like this.
//
//  ViewController.swift
//  InputDialogExample
//
//  Created by Belal Khan on 10/03/17.
//  Copyright © 2017 Belal Khan. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var labelMessage: UILabel!
    
    @IBAction func buttonPopup(_ 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.
    }
}

Creating iOS Dialog Box with Input using UIAlertController

  • Now the main part of this tutorial. We will create a function that will present our input dialog.
  • So inside your class create a function named showInputDialog().
    func showInputDialog() {
        //Creating UIAlertController and
        //Setting title and message for the alert dialog
        let alertController = UIAlertController(title: "Enter details?", message: "Enter your name and email", preferredStyle: .alert)
        
        //the confirm action taking the inputs
        let confirmAction = UIAlertAction(title: "Enter", style: .default) { (_) in
            
            //getting the input values from user
            let name = alertController.textFields?[0].text
            let email = alertController.textFields?[1].text
            
            self.labelMessage.text = "Name: " + name! + "Email: " + email!
            
        }
        
        //the cancel action doing nothing
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }
        
        //adding textfields to our dialog box
        alertController.addTextField { (textField) in
            textField.placeholder = "Enter Name"
        }
        alertController.addTextField { (textField) in
            textField.placeholder = "Enter Email"
        }
        
        //adding the action to dialogbox
        alertController.addAction(confirmAction)
        alertController.addAction(cancelAction)
        
        //finally presenting the dialog box
        self.present(alertController, animated: true, completion: nil)
    }
  • Now you just need to call this function on button click. So it will be like.
    @IBAction func buttonPopup(_ sender: UIButton) {
        showInputDialog()
    }
  • Now you can try running the application.
iOS Dialog Box with Input

iOS Dialog Box with Input

  • Bingo! Its working absolutely fine.

So thats all for this iOS Dialog Box with Input tutorial friends. Let me know by your comments if you are having confusions or troubles on the code.

And if you found this post helpful then you can share this to help us. Thank You 🙂

Share this:

  • Tweet
  • Share on Tumblr
  • WhatsApp

Related

Filed Under: iOS Development Tutorial Tagged With: ios dialog box with input, ios input 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. ria says

    May 8, 2017 at 10:11 am

    THANK YOU FOR THOSE CODES THEY HELPED ME A LOT YOUR ARTICLE IS VERY INFORMATIVE

    Reply
  2. disha says

    May 11, 2017 at 10:31 am

    oh great and informative article keep on writing such articles thanks for the steps

    Reply
  3. geeta says

    May 24, 2017 at 10:46 am

    HANK FOR SHARING THIS ARTICLE IT WAS VERY USEFUL TO ME KEEP SHARING SUCH ARTICLES

    Reply
  4. dave says

    October 3, 2017 at 6:42 pm

    Thanks for the tips. Is there a way validate the fields before you allow the alert to be dismissed?

    Reply
  5. Louis Tr says

    October 13, 2017 at 12:29 am

    How can we valid email address and display warning message in the UIAlertController ?

    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 PHP MySQL Tutorial – Connecting iOS App… (94,532)
  • Swift SQLite Tutorial for Beginners – Using… (94,175)
  • UIWebView Example to Load URL in iOS using Swift in Xcode (78,244)
  • Xcode Login Screen Example using Swift 3, PHP and MySQL (65,225)
  • Download Full High Sierra Installer to Create Bootable USB (61,246)
  • How to Format USB on Mac? Formatting External Hard… (60,779)
  • Swift JSON Tutorial – Fetching and Parsing… (58,674)
  • Firebase Realtime Database Tutorial for Swift using Xcode (52,001)
  • iOS Registration Form Example using PHP and MySQL (46,976)
  • Xcode Text Field Tutorial for iOS Application using Swift (39,039)




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·