DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Key Features of Swift Programming Language

Trending

  • From Issue to Reviewable Patch: Build a Sandboxed Coding Agent With Deep Agents and Automated Tests
  • Genkit Flows on Kubernetes, Routed by a Decision Model: senro + jev in Go
  • When Production Stops Moving: Running Claude Code Across a Distributed Enterprise Integration Team
  • A Non-Expiring AKS Deployment Token Could Read Secrets Across Three Namespaces

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

Input Alert in Objective-C

Allow for input alerts in your iOS applications.

By 
Hitanshi Mehta
·
Updated Aug. 12, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
13.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Introduction

We have seen an input alert in iOS apps that look like this:

Input Alert In Xcode Using Objective-C

Example input alert


In this article, we will learn how to create an input alert in Objective-C using Xcode. I’ve written this article for beginners. If you are familiar with Xcode, then add one button, provide touch-up inside, and jump to step seven. 

Step One

Object library

Object library


Open up the object library, find a button. We will show input alert on a button click, so drag the button to a storyboard.

Step Two

Adjusting button

Adjusting button

Step Three

Add missing constraints

Add missing constraints

Provide constraints to resolve any layout issues.

If a warning is still present, then select update missing constraints.

Update Constraint Constants

Update constraint constants

Step Four

 Image title

To uniquely identify any event on the component, we need to name the event. To provide a unique name to the event, open ViewController.h and Main.storyboard at the same time with the help of an assistant editor.

Step Five

Adding button to ViewController.h

Adding button to View Controller

Right-click on the button. Selecting touch up inside will allow you to perform any task on a button click.

Step Six

Naming button

Allowing button to receive action


Once you release the cursor, the following pop-up will appear. Here, you can provide a unique name for the event. Then, click connect.

Our designing is done; now we will perform a coding task.

Step Seven

Input alert

Input alert


In the alert box, we need four things.

1. Alertbox With Heading

- (void)viewDidLoad {
  [super viewDidLoad];
}

-(void)ShowInputAlertWithMsg:(NSString *)msg
{
  UIAlertController *alertVC=[UIAlertController alertControllerWithTitle:@”Login” message:msg preferredStyle:UIAlertControllerStyleAlert];
}


First, create a method that takes one string parameter.

In that method, create an instance of UIAlertController, which provides a heading and displays a text message. You will pass this text message as an argument.

2. Textfield

[alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  {
    textField.placeholder=@”UserName”;
    textField.textColor=[UIColor redColor];
    textField.clearButtonMode=UITextFieldViewModeWhileEditing;
  }
}];

[alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  textField.placeholder=@”Password”;
  textField.textColor=[UIColor redColor];
  textField.clearButtonMode=UITextFieldViewModeWhileEditing;
  textField.secureTextEntry=true;
}];
}


In the alert box, we want to add two text fields — one for a username and another for a password. Double click on addTextFieldWithConfigurationHandler. This will allow you to set a property for a text field.

3. Dismiss Button

UIAlertAction *action=[UIAlertAction actionWithTitle:@”Login” style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  NSLog(@”Login Tapped”);
  NSString *username=alertVC.textFields[0].text;
  NSString *password=alertVC.textFields[1].text;
  NSLog(@”%@ %@”,username,password);
}];


Create an instance of UIAlertAction. This provides a title and default style.

Double click on the handler. In the handler, you can write code to ensure that an action is performed.

We have created an action and an alert, but they are not connected. We can connect them using addAction.

[alertVC addAction:action];
[self presentViewController:alertVC animated:true completion:nil];
}


We need to connect an action controller to the current view controller using presentViewController.

4. Information Message

- (IBAction)btnInputAlert:(id)sender {
  [self ShowInputAlertWithMsg:@”PLease Login”];
}


We have created an alert controller with an action (Dismiss button). Now we will provide an information message. To do so, call a method from a button touchup inside the event. While calling the method, provide an information message as an argument.

Output

Simulating completed input alert

Simulating completed input alert


Console for simulated input alert

Console output for simulated input alert


I hope that the concept is clear to you. If you have any questions, ask in the comment section!!

Objective C

Opinions expressed by DZone contributors are their own.

Related

  • Key Features of Swift Programming Language

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook