Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How can I get text from a text field and display it in an alert?

Hello! I am BRAND NEW to Objective-C and Xcode and iOS, so PLEASE be forgiving of what is going to be a stupid question. I have a text field. I want to enter some text, click "return", and see my text in an alert. It doesn't serve any useful purpose, but this is how I learn. So far, I am unsuccessful. Here's my function from my HelloEarthViewController.m file:



-(IBAction)showInputMessageUITextField *)textField
{
if ([textField.text isEqualToString:@""])
{
    return;
}
UIAlertView *helloEarthInputAlert = [[UIAlertView alloc]
                                     initWithTitle:@"Name!" message:[NSString stringWithFormat:@"Message: %@", textField.text]
                                     delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
// Display this message.
[helloEarthInputAlert show];

}


Then I go option+click the text field and drag it to File's Owner and connect it to "showInputMessage:" Then I run it and the build succeeds, but nothing happens when I click my "return" button on the iPhone simulator.


What have I missed? Where have I gone wrong?

MacBook Pro, OS X Mountain Lion (10.8.3)

Posted on Jul 9, 2013 9:08 PM

Reply
6 replies

Jul 9, 2013 10:23 PM in response to Jeremy Coulson

It may be different for me in OSX.


You need to declare an IBOutlet variable in the interface which you will use to communicate with the text field. You send messages through the IBOutlet variable to 'get' and 'set' values in the text field.


You need an action button to connect to your action selector. The action is where you can communicate with the text field and use the data.

Jul 10, 2013 6:11 AM in response to mark133

Okay, here's where I am. I have this in my view controller .h file:



@interface HelloEarthViewController : UIViewController <UITextFieldDelegate>
{
    IBOutlet UITextField *textFieldTest;
}

@end


And this in my view controller .m file:



-(IBAction)showInputMessage
{
    if ([textFieldTest.text isEqualToString:@""])
    {
        return;
    }
    UIAlertView *helloEarthInputAlert = [[UIAlertView alloc]
                                         initWithTitle:@"Name!" message:[NSString stringWithFormat:@"Message: %@", textFieldTest.text]
                                         delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    // Display this message.
    [helloEarthInputAlert show];
    
}


And I have connected the text field to showInputMessage. When I hit "return", however, I still get nothing.


Is an "action button" a real button? From my limited understanding, the alert should launch when I click "return" since showInputMessage is attached to Editing Did End on the text field.


Thanks for the help. The transition from writing C# for web applications to writing Objective-C for iOS applications is not going as smoothly as I'd anticipated.

Jul 10, 2013 6:49 AM in response to Jeremy Coulson

From a debugging standpoint, with your code as written you have no way to know if showInputMessage is not being called when you press the button or if the if test in showInputMessage is always returning true to the test for an empty string.


I would either add a display message before the return, something like "text field is empty" as the message or else put a breakpoint in there to see what path your code is taking.


Once you know if the problem is the button press isn't sending the showInputMessage message or if the if test is always returning true you can proceed.


Note this is meant to help you learn to debug your code rather then give you an answer to this specific problem.


regards

Jul 10, 2013 9:24 AM in response to Frank Caggiano

Okay, here are the two things I did based on Frank's suggestions.


First, I added an alert to the test, but I don't get anything from it regardless of the emptiness or filledness of the field:


if ([textFieldTest.text isEqualToString:@""])
    {
        UIAlertView *helloEarthAlert = [[UIAlertView alloc]
                                        initWithTitle:@"Test!" message:@"Empty."
                                        delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [helloEarthAlert show];
        return;
    }
    else
    {
        UIAlertView *helloEarthAlert = [[UIAlertView alloc]
                                        initWithTitle:@"Test!" message:@"Not empty."
                                        delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [helloEarthAlert show];
        return;
    }


Then, I added a breakpoint right on -(IBAction)showInputMessage. If my understanding is correct, the program should stop execution there and wait for me if that function is actually getting called by the "return" button. The breakpoint seems to be ignored, so either I'm using it incorrectly or that "return" button never actually executes the showInputMessage code.


Yes? No?

Jul 10, 2013 10:01 PM in response to Jeremy Coulson

Your understanding regarding breakpoints is correct. It's important that the breakpoint symbol is darkblue and not lightblue as a lightblue symbol indicates an inactive breakpoint.


I must admit I've never used controlEvents of an UITextField. I always assign a delegate (usually the viewController, which controls the view, where the textfield is in) and implement one or more delegate methods.


Dirk

How can I get text from a text field and display it in an alert?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.