OK I am trying to retrieve a variable from this text field from this example http://junecloud.com/journal/code/displaying-a-password-or-text-entry-prompt-on-the-iphone.html. He says to do this :
"To get the text entered you just need to set a delegate for the text field and the alert, as shown in the example code above. Then you can use textFieldDidEndEditing: to get the value and store it somewhere temporary. When alertView:didDismissWithButtonIndex: is called, you can look up the saved value, and either use it or discard it depending on what button was pressed."
Thing is I'm so new to iOS and objective c that this means nothing to me. To me the text field delegate is set to self-- passwordField.delegate = self; Does anyone have an example to show? So I can see how to retrieve the entered text.
UIAlertView *passwordAlert = [[UIAlertView alloc] initWithTitle:#"Phone Number" message:#"\n\n\n"
delegate:self cancelButtonTitle:NSLocalizedString(#"Cancel",nil) otherButtonTitles:NSLocalizedString(#"OK",nil), nil];
UILabel *passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(12,40,260,25)];
passwordLabel.font = [UIFont systemFontOfSize:16];
passwordLabel.textColor = [UIColor whiteColor];
passwordLabel.backgroundColor = [UIColor clearColor];
passwordLabel.shadowColor = [UIColor blackColor];
passwordLabel.shadowOffset = CGSizeMake(0,-1);
passwordLabel.textAlignment = UITextAlignmentCenter;
passwordLabel.text =#"Cell Phone Number xxx-xxx-xxxx";
[passwordAlert addSubview:passwordLabel];
UIImageView *passwordImage = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"passwordfield" ofType:#"png"]]];
passwordImage.frame = CGRectMake(11,79,262,31);
[passwordAlert addSubview:passwordImage];
UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(16,83,252,25)];
passwordField.font = [UIFont systemFontOfSize:18];
passwordField.backgroundColor = [UIColor whiteColor];
passwordField.secureTextEntry = YES;
passwordField.keyboardAppearance = UIKeyboardAppearanceAlert;
passwordField.delegate = self;
[passwordField becomeFirstResponder];
[passwordAlert addSubview:passwordField];
[passwordAlert setTransform:CGAffineTransformMakeTranslation(0,9)];
[passwordAlert show];
[passwordAlert release];
[passwordField release];
[passwordImage release];
[passwordLabel release];
Create the textFieldDidEndEditing: delegate method in the same class:
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSString *textValue = textField.text;
NSLog(#"Value: %#", textValue);
}
Related
Got the following code:
-(void)addGrade {
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:#"Text" message:#"\n \n \n" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Add", nil];
// Textfield1
UITextField *utextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 50.0, 260.0, 25.0)];
[utextfield becomeFirstResponder];
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
utextfield.leftView = paddingView;
utextfield.leftViewMode = UITextFieldViewModeAlways;
utextfield.placeholder = #"Subject";
[utextfield setBackgroundColor:[UIColor whiteColor]];
utextfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[alertview addSubview:utextfield];
// Textfield2
UITextField *ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 85.0, 90.0, 25.0)];
ptextfield.placeholder = #"Another Subject";
ptextfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
ptextfield.textAlignment = NSTextAlignmentCenter;
[ptextfield setBackgroundColor:[UIColor whiteColor]];
[alertview addSubview:ptextfield];
// Textfield3
UITextField *ctextfield = [[UITextField alloc] initWithFrame:CGRectMake(161.0, 85.0, 27.0, 25.0)];
ctextfield.placeholder = #"3";
[ctextfield setBackgroundColor:[UIColor whiteColor]];
ctextfield.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
ctextfield.textAlignment = NSTextAlignmentCenter;
[alertview addSubview:ctextfield];
// Label1
UILabel *telt = [[UILabel alloc] initWithFrame:CGRectMake(121.0, 85.0, 40.0, 25.0)];
telt.text = #"Text";
telt.textColor = [UIColor whiteColor];
telt.backgroundColor = [UIColor clearColor];
[alertview addSubview:telt];
// Label2
UILabel *keermee = [[UILabel alloc] initWithFrame:CGRectMake(199.0, 85.0, 100.0, 25.0)];
keermee.text = #"more text";
keermee.textColor = [UIColor whiteColor];
keermee.backgroundColor = [UIColor clearColor];
[alertview addSubview:keermee];
[alertview show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:#"Add"])
{
UITextField *field = (UITextField *)[[alertView subviews] lastObject];
NSLog (#"%#", field.text);
}
}
This code makes 3 Textfields and 2 labels in an UIAlertView.
With UITextField *field = (UITextField *)[[alertView subviews] lastObject]; I got back "more text" which is true because you are asking about the last object.
Now is my question how to get the content of all the UITextfield not only the label?
for (UIView *view in [alertView subviews]){
if ([view isMemberOfClass:[UITextField class]]){
UITextField *textField = (UITextField *)view;
NSLog(#"%#", textField.text);
}
}
Assign tags and retrieve them with those.
myTextField.tag = 100;
Then to find it use this function:
myTextField = [view subviewWithTag: 100];
I am trying to get 3 different values from 3 different Uitextfields and then save them into 1 string to send it to a rest API server through a button action. I am stucked on how to get all 3 texts inputs from the 3 UItextfields.Any thought? My textfields are account,username,password.
UITextField *account = [[UITextField alloc] initWithFrame:CGRectMake(90, 50, 140, 30)];
account.borderStyle = UITextBorderStyleRoundedRect;
account.font = [UIFont systemFontOfSize:14];
account.placeholder = #"Account";
account.autocorrectionType = UITextAutocorrectionTypeNo;
account.keyboardType = UIKeyboardTypeDefault;
account.returnKeyType = UIReturnKeyDone;
account.clearButtonMode = UITextFieldViewModeWhileEditing;
account.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
account.delegate = self;
[self.view addSubview:account];
[account release];
UITextField *username = [[UITextField alloc] initWithFrame:CGRectMake(90, 80, 140, 30)];
username.borderStyle = UITextBorderStyleRoundedRect;
username.font = [UIFont systemFontOfSize:14];
username.placeholder = #"User ID";
username.autocorrectionType = UITextAutocorrectionTypeNo;
username.keyboardType = UIKeyboardTypeDefault;
username.returnKeyType = UIReturnKeyDone;
username.clearButtonMode = UITextFieldViewModeWhileEditing;
username.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
username.delegate = self;
[self.view addSubview:username];
[username release];
UITextField *password = [[UITextField alloc] initWithFrame:CGRectMake(90, 110, 140, 30)];
password.borderStyle = UITextBorderStyleRoundedRect;
password.font = [UIFont systemFontOfSize:14];
password.placeholder = #"Password";
password.autocorrectionType = UITextAutocorrectionTypeNo;
password.keyboardType = UIKeyboardTypeDefault;
password.secureTextEntry = YES;
password.returnKeyType = UIReturnKeyDone;
password.clearButtonMode = UITextFieldViewModeWhileEditing;
password.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
password.delegate = self;
[self.view addSubview:password];
[password release];
Can't find away to get all 3 datas when using the :
-(void) textFieldDidEndEditing:(UITextField *)textField
function.
Thanks in advance!
Set textfield tag
userNameTextField.tag = 1
accountTextField.tag = 2
passwordTextField.tag = 3
Save the values in shouldChangeCharactersInRange
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
switch (textField.tag) {
case 1:
//userNameTextField
NSString *userNameTextField = [NSString stringWithFormat:#"%#%#",textField.text,string];
//save
break;
case 2:
//accountTextField
NSString *accountTextField = [NSString stringWithFormat:#"%#%#",textField.text,string];
//save
break;
case 3:
//passwordTextField
NSString *passwordTextField = [NSString stringWithFormat:#"%#%#",textField.text,string];
//save
break;
default:
break;
}
}
you should keep the UITextFields as ivars, as well as correlating NSStrings (userNameString, passwordString, accountString). then on the following method you should check which textField invoked your delegate and assign the textField's text to the correct string. you can also use the textfield tag property to differentiate between them, and then you don't need to keep them as ivars..
-(void) textFieldDidEndEditing:(UITextField *)textField{
if (textField==userName)
userNameString = textField.text;
if (textField== account)
accountString = textField.text
if (textField== password)
password String = textField.text;
}
give each text field a tag, e.g. 1, 2 and 3
in textFieldDidEndEditing: distinguish the three text fields by tag:
.
if (textField.tag == 1) {
// assign the text to the right variable
}
... etc.
If you follow this pattern, you can avoid cluttering your class with many properties.
To make the code more readable, you can use this pattern:
#define userNameTextField 1
#define accountTextField 2
#define passwordTextField 3
or
typedef enum {
userNameTextField = 1,
accountTextField,
passwordTextField
} TextFields;
I've got a view with 2 UITextFields on it. I fill in both fields and press a button which calls this code below.
userNameStr =[NSString stringWithString:textFieldRounded.text];
clubFanStr = [NSString stringWithString:clubNameTextField.text];
NSLog(#"un: %#", userNameStr);
NSLog(#"cb: %#", clubFanStr);
The NSLogs show exactly what was typed into the fields.
I then press another button, a 3rd UITextField appears I put text in that, press a button and it saves, same as above.
Later on in the app when clubFanStr is used in an NSLog it crashes the app. Sometimes it doesnt crash the app if whatever is contained in clubFanStr seems to be somewhat valid.
userNameStr never crashes the app at the same point.
So I though this was strange at changed the code to this
userNameStr =[NSString stringWithString:clubNameTextField.text];
clubFanStr = [NSString stringWithString:textFieldRounded.text];
NSLog(#"un: %#", userNameStr);
NSLog(#"cb: %#", clubFanStr);
Basically swapping which string holds data from which UITextField.
So now app crashes later on when i try NSLog(#"%#",userNameStr);
So it seems that only the UITextField clubNameTextField is malfunctioning.
Here is code of both of there declarations.
textFieldRounded = [[UITextField alloc] initWithFrame:CGRectMake(5, 40, 310, 30)];
textFieldRounded.borderStyle = UITextBorderStyleRoundedRect;
textFieldRounded.textColor = [UIColor blackColor]; //text color
textFieldRounded.font = [UIFont systemFontOfSize:17.0]; //font size
textFieldRounded.placeholder = #"User Name?"; //place holder
textFieldRounded.backgroundColor = [UIColor whiteColor]; //background color
textFieldRounded.autocorrectionType = UITextAutocorrectionTypeNo;
textFieldRounded.keyboardType = UIKeyboardTypeDefault; // type of the keyboard
textFieldRounded.returnKeyType = UIReturnKeyDone; // type of the return key
textFieldRounded.clearButtonMode = UITextFieldViewModeWhileEditing;
textFieldRounded.delegate = self;
textFieldRounded.hidden = YES;
[self.view addSubview:textFieldRounded];
clubNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(5, 120, 310, 30)];
clubNameTextField.borderStyle = UITextBorderStyleRoundedRect;
clubNameTextField.textColor = [UIColor blackColor]; //text color
clubNameTextField.font = [UIFont systemFontOfSize:17.0]; //font size
clubNameTextField.placeholder = #"Team you support?"; //place holder
clubNameTextField.backgroundColor = [UIColor whiteColor]; //background color
clubNameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
clubNameTextField.keyboardType = UIKeyboardTypeDefault; // type of the keyboard
clubNameTextField.returnKeyType = UIReturnKeyDone; // type of the return key
clubNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
clubNameTextField.delegate = self;
clubNameTextField.hidden = YES;
//textFieldRounded.editing lets you know of the text field is being currently edited
[self.view addSubview:clubNameTextField];
Anybody able to clue me into what I've done wrong?
Many Thanks,
-Code
Instead of
userNameStr =[NSString stringWithString:textFieldRounded.text];
clubFanStr = [NSString stringWithString:clubNameTextField.text];
try
userNameStr = [[NSString alloc] initWithString:textFieldRounded.text];
clubFanStr = [[NSString alloc] initWithString:clubNameTextField.text];
Is clubFanStr used in another method? You may want to retain it then, and the userNameStr as well.
Later on in the app when clubFanStr is used in an NSLog it crashes the app
This almost certainly means that you have a reference counting error, and the string has been deallocated. How are userNameStr and clubFanStr declared? You are assigning autoreleased values to them; either they are retain/copy properties, and you should be using self.<propertyName> to assign to, or they aren't and you should be retaining as you assign.
//---------Searchoing----- //
- (IBAction) Search_hadit
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Search"
message:#" "
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Search",
nil];
CGRect frame = CGRectMake(14, 35, 255, 23);
myTextField = [[UITextField alloc] initWithFrame:frame];
myTextField.borderStyle = UITextBorderStyleBezel;
myTextField.textColor = [UIColor blackColor];
myTextField.textAlignment = UITextAlignmentCenter;
myTextField.font = [UIFont systemFontOfSize:14.0];
myTextField.placeholder = #"Enter Query";
myTextField.backgroundColor = [UIColor whiteColor];
myTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
myTextField.keyboardType = UIKeyboardTypeDefault;// use the default type input method (entire keyboard)
myTextField.returnKeyType = UIReturnKeyDone;
myTextField.delegate = self;
myTextField.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
[alert addSubview:myTextField];
[alert show];
[alert release];
}
When I click search button in my app alert view show...
I want when user click search button I get the text which he enter and send this data to another function on as he click search button where I can perform search. Any idea?
I guess (acording to the title) you'll need an UIAlertViewDelegate or at least the function:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString* buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle equalToString:#"Search"]) {
[do something];
}
}
But the text in your description describes a different problem than your title. I can't provide a solution to that, though.
I have an app with tabs and navigation controllers.
Everything works great except a UIActionSheet. I even have a UIAlertView taht shows fine in the same controller, but the action sheet doesn't show. The screen goes dark, like it's showing, but no view.
Here's the relevant code:
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:#"Erase the file?"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Clear List"
otherButtonTitles:nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
//[actionSheet showInView:self.view];
//MyAppDelegate* delegate = [[UIApplication sharedApplication] delegate];
//[actionSheet showInView:delegate.tabBarController.view];
[actionSheet release];
The commented out code was the different ways of showing it that I've tried.
Any thoughts as to why this isn't working?
[actionSheet showInView:self.view.window];
Don't show it from the current keyWindow - show it from the actual tab bar with something like
[actionSheet showFromTabBar:delegate.tabBarController.tabBar];
Try, this works perfectly:
UITextField *tempTextField;
UIActionSheet *myActionSheet;
- (IBAction) myDatePickerAction:(id)sender {
tempTextField = (UITextField *)sender;
[(UITextField *)sender resignFirstResponder];
myActionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[myActionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
UIDatePicker *theDatePicker = [[UIDatePicker alloc] init];
theDatePicker.datePickerMode = UIDatePickerModeDate;
CGRect datePickRect = theDatePicker.frame;
datePickRect.size.height = 120;
theDatePicker.frame = datePickRect;
[theDatePicker addTarget:self action:#selector(setDateValue:) forControlEvents:UIControlEventValueChanged];
[myActionSheet addSubview:theDatePicker];
[theDatePicker release];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Close"]];
closeButton.momentary = YES;
CGRect closeButtonRect = closeButton.frame;
closeButtonRect.size.width = 50;
closeButtonRect.size.height = 30;
closeButton.frame = closeButtonRect;
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[myActionSheet addSubview:closeButton];
[closeButton release];
[myActionSheet showFromTabBar:appDelegate.tabBarController.tabBar];
CGRect actionSheetRect = myActionSheet.bounds;
actionSheetRect.size.height = 300;
myActionSheet.bounds = actionSheetRect;
}
- (IBAction)setDateValue:(id)sender {
NSDateFormatter *myDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[myDateFormatter setDateFormat:#"MM/dd/yyyy"];
NSLog(#"Formatted date1:%#",[myDateFormatter stringFromDate:[(UIDatePicker *)sender date]]);
tempTextField.text = [myDateFormatter stringFromDate:[(UIDatePicker *)sender date]];
tempTextField = nil;
}
- (IBAction)dismissActionSheet:(id)sender {
[myActionSheet dismissWithClickedButtonIndex:0 animated:YES];
myActionSheet = nil;
//[myActionSheet release];
//Do not release here. Just set to nil and release it in dealloc.
}