Showing a alertview with a textbox in iPhone - iphone

Is it possible to show an alertview with a textbox inside like the AppStore app.
It asks for password in such a dialog.
I've seen atleast a couple of other third party apps using it. Is it a private API?

Here's an "Apple Approved" way of doing it from Tharindu Madushana. I got it from his comment in this page: http://iosdevelopertips.com/undocumented/alert-with-textfields.html
// Ask for Username and password.
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:#"Twitter Details!" message:#"\n \n \n" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
// Adds a username Field
UITextField *utextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
utextfield.placeholder = #"Username";
[utextfield setBackgroundColor:[UIColor whiteColor]];
[alertview addSubview:utextfield];
// Adds a password Field
UITextField *ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 80.0, 260.0, 25.0)];
ptextfield.placeholder = #"Password";
[ptextfield setSecureTextEntry:YES];
[ptextfield setBackgroundColor:[UIColor whiteColor]]; [alertview addSubview:ptextfield];
// Move a little to show up the keyboard
CGAffineTransform transform = CGAffineTransformMakeTranslation(0.0, 80.0);
[alertview setTransform:transform];
// Show alert on screen.
[alertview show];
[alertview release];
//...
// Don't forget to release these after getting their values
[utextfield release];
[ptextfield release];
And finally to get the text back
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
return; //Cancel
UITextField *field = (UITextField *)[[alertView subviews] lastObject];
NSLog (#"%#", field.text);
}

Yes, it's undocumented. To add a text field to UIAlertView, use addTextFieldWithValue: label: method. You call with the default text as the first argument and the text that displays in an empty field as the second. Having done that, you can access the field via using textFieldAtIndex:n - see below.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Who are you?"
message:#"Give your full name"
delegate:self cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[alert addTextFieldWithValue:#""label:#"Name"];
// Customise name field
UITextField* name = [alert textFieldAtIndex:0];
name.clearButtonMode = UITextFieldViewModeWhileEditing;
name.keyboardType = UIKeyboardTypeAlphabet;
name.keyboardAppearance = UIKeyboardAppearanceAlert;
[alert show];
The next snippet shows how to retrieve the value in the name field:
NSLog("Name is %#", [[modalView textFieldAtIndex:0] text]);

This is a really old questions with really old answers.
This is a sample of how I get a UITextfield into a UIAlertView since ios 5:
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"New List Name" message:#"" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"Continue", nil];
message.alertViewStyle = UIAlertViewStylePlainTextInput;
self.alertTextField = [message textFieldAtIndex:0];
self.alertTextField.keyboardType = UIKeyboardTypeAlphabet;
message.delegate = self;
[message show];
[self.alertTextField becomeFirstResponder];
where alertTextField was set up like this:
#property (nonatomic, strong) UITextField *alertTextField;

Jeff Lamarche posted some sample code on his blog to do just this. The formatting looked a bit wonky when I tried it but it's probably a good starting point.

Related

how to access the secureTextEntry for further use?

Hi I'm using the below code which includes the alertView containing UserID and Password field.
these UserID and Password are text field, i made password field as secure, but when i later want to access the data entered in password field, its showing no value in password field..
My code is
self.loginPassword = [[ UIAlertView alloc] initWithTitle:#"Please Login" message:#"Enter Valid UserID and Password\n\n\n\n" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Cancel", nil];
//[self.loginPassword setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
self.userIDText = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 80.0, 260.0, 25.0)];
self.userIDText.text=#"";
[self.userIDText setBackgroundColor:[UIColor whiteColor]];
[self.userIDText setKeyboardAppearance:UIKeyboardAppearanceAlert];
[self.userIDText setAutocorrectionType:UITextAutocorrectionTypeNo];
[self.userIDText setTextAlignment:UITextAlignmentLeft];
self.passwordField = [[UITextField alloc] initWithFrame:CGRectMake(12.0,115.0, 260.0, 25.0)];
self.passwordField.text=#"";
[self.passwordField setBackgroundColor:[UIColor whiteColor]];
[self.passwordField setKeyboardAppearance:UIKeyboardAppearanceAlert];
[self.passwordField setAutocorrectionType:UITextAutocorrectionTypeNo];
[self.passwordField setTextAlignment:UITextAlignmentLeft];
[self.loginPassword addSubview:self.userIDText];
[self.loginPassword addSubview:self.passwordField];
self.passwordField.secureTextEntry = YES;
[self.loginPassword setTag:2];
[self.loginPassword show];
[self.loginPassword release];
please suggest me some solution..
Create and In-built UIAlertView
UIAlertView *alert = [[UIAlertView alloc] init];
// Define Alertview Type to Login&Password
[alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
// Set Delegate to access Input Value
[alert setDelegate:self];
// show it
[alert show];
set Delegate in you interface (.h file) UIAlertViewDelegate
Implement This delegate method in your implementation (.m file)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Access your Username & Password from alertview and store them for further use.
NSString *username = [[alertView textFieldAtIndex:0] text];
NSString *password = [[alertView textFieldAtIndex:1] text];
NSLog(#"Username : %# , Password : %#",username, password);
}
This may help you.Just take a look at the code.
https://github.com/josecastillo/EGOTextFieldAlertView

How to generate multi-line rather than single line with UIAlertView?

This is my code
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Added to Cart" message:#"Some items are added for time being!" delegate:self cancelButtonTitle:#"View Cart" otherButtonTitles:#"Continue \n Shopping", nil];
alert.tag = 20;
[alert show];
I get output like this:
I need like this:
I have manual solution for you. But I think its not a good solution:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Added to Cart" message:#"Some items are added for time being!" delegate:self cancelButtonTitle:#"View Cart" otherButtonTitles:#"", nil];
UILabel *buttonTitle = [[UILabel alloc] initWithFrame:CGRectMake(148, 102, 125, 40)];
buttonTitle.text = #"Continue Shopping";
buttonTitle.font = [UIFont boldSystemFontOfSize:15];
buttonTitle.textColor = [UIColor whiteColor];
buttonTitle.textAlignment = UITextAlignmentCenter;
buttonTitle.backgroundColor = [UIColor clearColor];
buttonTitle.numberOfLines = 2;
[alert addSubview:buttonTitle];
alert.tag = 20;
[buttonTitle release];
[alert show];
You can use it anyway....
UIAlertView doesn't support multi-line buttons. One option would be to just use "Continue" instead of "Continue Shopping" as the button title, otherwise you'd have to use a custom alert view component, e.g. CODialog (you might have to customize it a bit to allow multi-line buttons, but it should be easy).
I think you can try to set numbersOfLine property to label on button. But i don`t know will apple aprove that.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Added to Cart" message:#"Some items are added for time being!" delegate:self cancelButtonTitle:#"View Cart" otherButtonTitles:#"Continue \n Shopping", nil];
NSArray *subviewsArray = [alert subviews];
for (UIView *subview in subviewsArray) {
if ([subview isKindOfClass:[UIButton class]]) {
NSArray *btnSubviews = [subview subviews];
for (UIView *btnSubview in btnSubviews) {
if ([btnSubview isKindOfClass:[UILabel class]]) {
UILabel *title = (UILabel *)btnSubview;
title.numberOfLines = 2;
}
}
}
}
alert.tag = 20;
[alert show];
[alert release];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Added to Cart" message:#"Some items are added for time being!" delegate:self cancelButtonTitle:#"View Cart" otherButtonTitles:#"Continue \n Shopping", nil];
alert.tag = 20;
[[[alert buttons] objectAtIndex:1] setLineBreakMode:UILineBreakModeWordWrap];
[alert show];
This code does exactly what you need.
Note, however that
- [UIAlertView buttons] is undocumented
- [UIButton setLineBreakMode] is deprecated. You can however, replace it with
[button.titleLabel setLineBreakMode] which is essentially the same.

Alert View show me warning

When I use alertView using below code, it shows me the warning
warning: Semantic Issue: Method '-addTextFieldWithValue:label:' not found (return type defaults to 'id')
Here is the code:
UIAlertView *alSave=[[UIAlertView alloc]initWithTitle:#"Save as" message:#"Title the note and click Save" delegate:self cancelButtonTitle:#"save" otherButtonTitles:#"cancel", nil];
NSArray *arr=[noteObj.noteTitle componentsSeparatedByString:#" - "];
app.longClickId = [noteObj.noteId integerValue];
[alSave addTextFieldWithValue:[NSString stringWithFormat:#"%#",[arr objectAtIndex:0]] label:#"Note Name"];
// show me warning at this place
textField = [alSave textFieldAtIndex:0];
textField.keyboardType = UIKeyboardTypeAlphabet;
textField.keyboardAppearance = UIKeyboardAppearanceAlert;
textField.autocorrectionType = UITextAutocorrectionTypeNo; // correction automatically
[alSave show];
if (app.NotePopOver!= nil) {
[app.NotePopOver dismissPopoverAnimated:YES];
}
[alSave release];
If you use a private method (of which addTextFieldWithValue: is one), then Apple will most likely reject your app. You can achieve the same result with the following snippet, courtesy of this answer which credits a no longer working link:
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Your title here!" message:#"this gets covered" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[myAlertView addSubview:myTextField];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 130.0);
[myAlertView setTransform:myTransform];
[myAlertView show];
[myAlertView release];
That method is undocumented. You will have to create your own text field and then add it to the alert view.

How to prompt user for text input in Alert view

I am writing a section of code where it would be best if I could use a pop up box something like UIAlertView and prompt the user to enter text like a password.
Any pointers on an elegant way of doing this?
Things are much simpler in iOS 5, just set the alertViewStyle property to the appropriate style (UIAlertViewStyleSecureTextInput, UIAlertViewStylePlainTextInput, or UIAlertViewStyleLoginAndPasswordInput). Example:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Password" message:#"Enter your password:" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
alertView.alertViewStyle = UIAlertViewStyleSecureTextInput;
UITextField *passwordTextField = [alertView textFieldAtIndex:0];
[alertView show];
> Simple You can apply like this
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Filename" message:#"Enter the file name:" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *passwordTextField = [alertView textFieldAtIndex:0];
[alertView show]
The best way that I've found to do this is to follow this tutorial: http://junecloud.com/journal/code/displaying-a-password-or-text-entry-prompt-on-the-iphone.html
The code used to achieve this is (taken directly from that great tutorial):
UIAlertView *passwordAlert = [[UIAlertView alloc] initWithTitle:#"Server Password" 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 = #"Account Name";
[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,109)];
[passwordAlert show];
[passwordAlert release];
[passwordField release];
[passwordImage release];
[passwordLabel release];
If my app was not to be released for yet a months or two, then I would login to http://developer.apple.com, look at the iOS 5 beta area, and see if UIAlertView might have something in store for us.
I think it would be helpful to know that UIAlertView is not modal so the alert will not block.
I ran into this problem where I wanted to prompt the user for input then continue and then use that input in the code after. But instead the code after the [alert show] would run first until you reached the end of the run loop then the alert would display.
Optimized code:
UIAlertView *passwordAlert = [[UIAlertView alloc] initWithTitle:#"Password"
message:#"Please enter the password:\n\n\n"
delegate:self
cancelButtonTitle:NSLocalizedString(#"Cancel",nil)
otherButtonTitles:NSLocalizedString(#"OK",nil), nil];
UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(16,83,252,25)];
passwordField.borderStyle = UITextBorderStyleRoundedRect;
passwordField.secureTextEntry = YES;
passwordField.keyboardAppearance = UIKeyboardAppearanceAlert;
passwordField.delegate = self;
[passwordField becomeFirstResponder];
[passwordAlert addSubview:passwordField];
[passwordAlert show];
[passwordAlert release];
[passwordField release];

Getting text from UIAlertView

I'm trying to get text from an alert view and add it to my mutable array to list in a table view. I realize there is a similar question that was posted a few months ago, but I dont understand how to utilize the given answer.
-(IBAction)insert {
UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:#"Enter Name"];
[dialog setMessage:#" "];
[dialog addButtonWithTitle:#"Cancel"];
[dialog addButtonWithTitle:#"OK"];
UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
[dialog show];
[data addObject:[nameField text]];
[mainTableView reloadData];
However my app crashes because it says I'm attempting to insert a nil object at index 0. What am I doing wrong?
EDIT: Ok I think I'm missing a method to handle the alertview. So I found this:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if([buttonTitle isEqualToString:#"Cancel"]) {
return;
}
else if([buttonTitle isEqualToString:#"Ok"]) {
[data addObject:nameField.text];
}
Now I just need to connect the pieces, but not sure how.
A common mistake that people make when using a UIAlertView is that they think that sending it a show message will block the main thread. It does not. Your code continues to execute, even though there is an alert on the screen. Thus, no value exists for the UITextField that you have passed in.
What you need to do is implement the UIAlertViewDelegate in your UIViewController to grab whatever a user has entered into the text field. That said, you still have to check for a nil value, because if you don't type anything in, the text value will be nil.
UPDATE: This answer was posted before Apple created an API for adding a UITextField to an alert through the UIAlertViewStyle. Here is the updated code, borrowed from #matt
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter Name"
message:#" "
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
Then, in the UIAlertViewDelegate call back:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
NSString *name = [alertView textFieldAtIndex:0].text;
// Insert whatever needs to be done with "name"
}
}
You don't need to add your own text field to the AlertView but instead set the appropriate style
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter Name"
message:#" "
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
Then access the entered text after OK (button 1) was pressed
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
NSString *name = [alertView textFieldAtIndex:0].text;
// name contains the entered value
}
}
Don't try to add a UITextView on a UIAlertView or else Apple might reject your app.
I already used the same kind of functionality in my app. But Apple REJECTED MY APP due to the use of UITextView in UIAlertView.
– alertView:clickedButtonAtIndex: is a delegate method of UIAlertView. Just add the method you defined in your edit to your controller implementation and you should be good to go, since you already set the controller to be the AlertView's delegate.
Also add to your class header, to avoid any delegate related warnings, ie:
#interface MyViewController : UIViewController <UIAlertViewDelegate> {
...
};
Don't forget to remove [data addObject:[nameField text]]; and [mainTableView reloadData]; from the insert method.
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:#"Alert title"
message:#"alert message"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
[myAlert addTextFieldWithValue:nil label:#"<place holder>"];
[[myAlert textField] setTextAlignment:UITextAlignmentCenter];
[[myAlert textField] becomeFirstResponder];
[myAlert show];
[myAlert release];
myAlert = nil;
No need to add a custom textfield. You can directly add using addTextFieldWithValue method.