Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8767261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:45:58+00:00 2026-06-13T16:45:58+00:00

I would like to run the same code if the button on the alert

  • 0

I would like to run the same code if the button on the alert is pressed, or the return key on the keyboard is pressed.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
works for the OK button on the alert, but I can not find an equivalent for the return key.

I have been experimenting with - (BOOL)textFieldShouldReturn:(UITextField *)alertTextField, to no avail.

A year ago, three people in the comments on this site had the same problem and it was not answered:

How do you get the alertview to respond to a “Return” on the keyboard with the same result as tapping the “OK” button? I can’t seem to find anything for the iOS 5 SDK on Google.

What are all the steps necessary to accomplish this?

Edit:

More specifically:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

    [[NSUserDefaults standardUserDefaults] setObject:[alertView textFieldAtIndex:0].text forKey:@"url_preference"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

When the user presses OK, that code is run. What is the similar, simple equivalent for the Return key on the keyboard?

Currently, I am trying this:

- (BOOL)textFieldShouldReturn:(UITextField *)alertTextField {
    [alertTextField resignFirstResponder];
    NSLog(@"test");
    return YES;
}

Which does nothing.

This is the definition of my alert:

-(void) noUrl {

    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Add a URL" message:@"Change it later in Settings" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Load",nil]; //If you change button name, change it in the other alertView function
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField * alertTextField = [alert textFieldAtIndex:0];
    alertTextField.keyboardType = UIKeyboardTypeURL;
    alertTextField.returnKeyType = UIReturnKeyGo;
    alertTextField.enablesReturnKeyAutomatically = YES;
    alertTextField.placeholder = @"http://www.example.com";
    [alert show];
    return;

}

If code needs to be added in another file, or another place, I would appreciate detailed specifics.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:45:59+00:00Added an answer on June 13, 2026 at 4:45 pm

    Set

    [alert textFieldAtIndex:0].delegate = self; 
    

    and then implement

    - (BOOL)textFieldShouldReturn:(UITextField *)alertTextField {//add any method you want to execute to here. This method will be called when user taps on the textfield in alertview.
     [alertTextField resignFirstResponder];// to dismiss the keyboard.
     [alert dismissWithClickedButtonIndex:0 animated:YES];//this is called on alertview to dismiss it.
    }
    

    Also put UITextFieldDelegate in your .h file as,

    @interface CustomViewController : UIViewController <UIAlertViewDelegate, UITextFieldDelegate> 
    

    As per the current edit in the question, you are missing,

    alertTextField.delegate = self; 
    

    Update:

    Based on your comment, make alert as a class level variable as,

    @interface CustomViewController : UIViewController <UIAlertViewDelegate, UITextFieldDelegate> {
      UIAlertView *alert;
    }
    

    Then change this method to,

    -(void) noUrl {
    
       alert = [[UIAlertView alloc] initWithTitle:@"Add a URL" message:@"Change it later in Settings" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Load",nil]; //If you change button name, change it in the other alertView function
       //rest of the code here...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got some heavyweight process, which does some video processing. I would like run
I would like to run a jar file extracted from my java project to
I would like to run this script (embed drive list in a site) that
I would like to run a script in production. This script generates a record
I would like to run my node.js application without the automatic logs it prints
I would like to run a script indefinitely. It look likes my current script,
I would like to run the following SQL select: SELECT ID, NUMERATOR, (SELECT m.COLUMNNAME
I would like to run APIDemos under platforms\android-1.5\samples directory on Android emulator but I
I would like to run a (extensive) query that produces one line of XML.
I would like to run a timer for every 5 hours and delete the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.