01-06-2012, 12:51 AM
#1 (permalink )
Registered Member
Join Date: Oct 2011
Location: Canada
Posts: 62
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
01-06-2012, 05:40 AM
#2 (permalink )
Registered Member
Join Date: Feb 2011
Location: Bucharest, Romania
Age: 22
Posts: 75
Quote:
Originally Posted by
aceiswild
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.
01-06-2012, 12:55 PM
#3 (permalink )
Registered Member
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
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.
01-06-2012, 12:59 PM
#4 (permalink )
Registered Member
Join Date: Oct 2011
Location: Canada
Posts: 62
Quote:
Originally Posted by
RickSDK
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!
01-07-2012, 04:05 AM
#5 (permalink )
New To iOS
Join Date: Nov 2011
Location: Waterloo, ON
Age: 20
Posts: 187
Quote:
Originally Posted by
aceiswild
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
01-07-2012, 04:35 AM
#6 (permalink )
Registered Member
Join Date: Oct 2011
Location: Canada
Posts: 62
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!
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» 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