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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Key Features of Swift Programming Language
  • Top 6 Programming Languages for Mobile App Development

Trending

  • Ethical AI in Agile
  • Agentic AI for Automated Application Security and Vulnerability Management
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • How to Practice TDD With Kotlin

Input Alert in Objective-C

Allow for input alerts in your iOS applications.

By 
Hitanshi Mehta user avatar
Hitanshi Mehta
·
Updated Aug. 12, 19 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
13.2K Views

Join the DZone community and get the full member experience.

Join For Free

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
  • Top 6 Programming Languages for Mobile App Development

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!