Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-06-2012, 12:51 AM   #1 (permalink)
Registered Member
 
aceiswild's Avatar
 
Join Date: Oct 2011
Location: Canada
Posts: 62
aceiswild is on a distinguished road
Default Textfield to NSString

Hey guys so I have an alert prompt containing a TextField where a person can enter their name. Once they have entered their name and click okay I need there name to show up as a String in the interface. But i can't seem to get it right tho i have done something similar before. Here is is my code for the alert:

Code:
// ALERT WITH TEXT
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your Name"
                                                    message:@"What is your name?\n\n"
                                                   delegate:self 
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"OK", nil];
    alert.tag = kTag_EnterNameAlert;
    
    CGRect entryFieldRect = CGRectZero;
    if( UIDeviceOrientationIsPortrait( [UIApplication sharedApplication].statusBarOrientation ) ) {
        entryFieldRect = CGRectMake(12, 110, 260, 25);
    }
    else {
        entryFieldRect = CGRectMake(12, 72, 260, 25);
    }
    
    UITextField *nameEntryField = [[UITextField alloc] initWithFrame:entryFieldRect];
    nameEntryField.tag = kTag_NameEmtryField;
    nameEntryField.backgroundColor = [UIColor whiteColor];
    nameEntryField.keyboardType = UIKeyboardTypePhonePad;
    nameEntryField.keyboardAppearance = UIKeyboardAppearanceAlert;
    nameEntryField.autocorrectionType = UITextAutocorrectionTypeNo;
    nameEntryField.clearButtonMode = UITextFieldViewModeWhileEditing;
    nameEntryField.placeholder = @"Name Here";
    [alert addSubview:nameEntryField];
    [nameEntryField becomeFirstResponder];
    
    [alert show];
    
    
    // ALERT WITH TEXT


-(void)alert:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 1) {
        
       // NSString Code to write name to interface here
      nameEntryField.text = [NSString stringWithFormat:@"Name: %@"];
        
    }
    
}
Thanks for ideas.
Steve
__________________
Don't be a quitter! Never give up and keep pushing to succeed at the things you love!

Last edited by aceiswild; 01-06-2012 at 12:58 AM. Reason: code change
aceiswild is offline   Reply With Quote
Old 01-06-2012, 05:40 AM   #2 (permalink)
Registered Member
 
Join Date: Feb 2011
Location: Bucharest, Romania
Age: 22
Posts: 75
nestedloop is on a distinguished road
Default

Quote:
Originally Posted by aceiswild View Post
Hey guys so I have an alert prompt containing a TextField where a person can enter their name. Once they have entered their name and click okay I need there name to show up as a String in the interface. But i can't seem to get it right tho i have done something similar before. Here is is my code for the alert:

Code:
// ALERT WITH TEXT
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your Name"
                                                    message:@"What is your name?\n\n"
                                                   delegate:self 
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"OK", nil];
    alert.tag = kTag_EnterNameAlert;
    
    CGRect entryFieldRect = CGRectZero;
    if( UIDeviceOrientationIsPortrait( [UIApplication sharedApplication].statusBarOrientation ) ) {
        entryFieldRect = CGRectMake(12, 110, 260, 25);
    }
    else {
        entryFieldRect = CGRectMake(12, 72, 260, 25);
    }
    
    UITextField *nameEntryField = [[UITextField alloc] initWithFrame:entryFieldRect];
    nameEntryField.tag = kTag_NameEmtryField;
    nameEntryField.backgroundColor = [UIColor whiteColor];
    nameEntryField.keyboardType = UIKeyboardTypePhonePad;
    nameEntryField.keyboardAppearance = UIKeyboardAppearanceAlert;
    nameEntryField.autocorrectionType = UITextAutocorrectionTypeNo;
    nameEntryField.clearButtonMode = UITextFieldViewModeWhileEditing;
    nameEntryField.placeholder = @"Name Here";
    [alert addSubview:nameEntryField];
    [nameEntryField becomeFirstResponder];
    
    [alert show];
    
    
    // ALERT WITH TEXT


-(void)alert:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 1) {
        
       // NSString Code to write name to interface here
      nameEntryField.text = [NSString stringWithFormat:@"Name: %@"];
        
    }
    
}
Thanks for ideas.
Steve
Several issues here.
First, what you are doing in the clickedButtonAtIndex is setting the value in the alert textfield to null, which I think you don't want.

You should do something along the lines of:
Code:
NSString * someString = ((UITextField*)[alert viewWithTag:kTag_NameEmtryField]).text;
Also, when you format the string with %@ you should enter an argument, like:

[...] [NSString stringWithFormat:@"Name: %@", nameString];

Hope this helps.
Cheers.
nestedloop is offline   Reply With Quote
Old 01-06-2012, 12:55 PM   #3 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
RickSDK is on a distinguished road
Default

your line: [NSString stringWithFormat:@"Name: %@"];

is missing the parameter.

also you have the line: if (buttonIndex == 1)

are you sure you are looking for 1 and not 0?

and finally, make sure you have the text field connected via interface manager.
__________________
Check out my apps

RickSDK is offline   Reply With Quote
Old 01-06-2012, 12:59 PM   #4 (permalink)
Registered Member
 
aceiswild's Avatar
 
Join Date: Oct 2011
Location: Canada
Posts: 62
aceiswild is on a distinguished road
Default

Quote:
Originally Posted by RickSDK View Post
your line: [NSString stringWithFormat:@"Name: %@"];

is missing the parameter.

also you have the line: if (buttonIndex == 1)

are you sure you are looking for 1 and not 0?

and finally, make sure you have the text field connected via interface manager.
Yes i have changed that 1 value to 0. But you can't connect an Alert with a textfield in the interface builder. Just have to change some things around and it should be good.
__________________
Don't be a quitter! Never give up and keep pushing to succeed at the things you love!
aceiswild is offline   Reply With Quote
Old 01-07-2012, 04:05 AM   #5 (permalink)
New To iOS
 
Join Date: Nov 2011
Location: Waterloo, ON
Age: 20
Posts: 187
RileyE is on a distinguished road
Default

Quote:
Originally Posted by aceiswild View Post
Yes i have changed that 1 value to 0. But you can't connect an Alert with a textfield in the interface builder. Just have to change some things around and it should be good.
This may help
RileyE is offline   Reply With Quote
Old 01-07-2012, 04:35 AM   #6 (permalink)
Registered Member
 
aceiswild's Avatar
 
Join Date: Oct 2011
Location: Canada
Posts: 62
aceiswild is on a distinguished road
Default

Thanks!
Apparently apple can reject your app if you have a textfield in an alert?
Has anybody had this happen?
__________________
Don't be a quitter! Never give up and keep pushing to succeed at the things you love!
aceiswild is offline   Reply With Quote
Reply

Bookmarks

Tags
alert, prompt, string

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 402
10 members and 392 guests
buggen, guusleijsten, j.b.rajesh@gmail.com, morterbaher, QuantumDoja, sacha1996, Sami Gh, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,673
Threads: 94,122
Posts: 402,907
Top Poster: BrianSlick (7,990)
Welcome to our newest member, morterbaher
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:35 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0