I have three UITextfields in UIAlertView, while tap on one UITextfield and try to tap on other its not selecting and creating a problem, also problem of resigning first responder, is it not good choice of using UITextfield in UIAlertView
- (IBAction)heightMethod:(id)sender
{
self.centimeterTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
centimeterTextField.placeholder = #" Centimeters";
self.centimeterTextField.delegate=self;
self.centimeterTextField.tag=3;
[ self.centimeterTextField setBackgroundColor:[UIColor whiteColor]];
[self.alertHeight addSubview: self.centimeterTextField];
self.ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(40, 80.0, 80, 25.0)]; ptextfield.placeholder = #" Feet";
self.ptextfield.delegate=self;
self.ptextfield.tag=4;
[self.ptextfield setBackgroundColor:[UIColor whiteColor]];
[self.alertHeight addSubview:self.ptextfield];
self.ptextfieldInches = [[UITextField alloc] initWithFrame:CGRectMake(140, 80.0, 80, 25.0)]; ptextfieldInches.placeholder = #" Inches";
self.ptextfieldInches.delegate=self;
self.ptextfieldInches.tag=5;
[ptextfieldInches setBackgroundColor:[UIColor whiteColor]];
[self.alertHeight addSubview:ptextfieldInches];
[self.centimeterTextField setKeyboardType:UIKeyboardTypeDecimalPad];
[self.ptextfieldInches setKeyboardType:UIKeyboardTypeDecimalPad];
[self.ptextfield setKeyboardType:UIKeyboardTypeDecimalPad];
self.alertHeight.tag=1;
[self.alertHeight show];
}
- (void) presentSheet {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Enter Information"
message:#"Specify the Name and URL" delegate:self cancelButtonTitle:#"Cancel"otherButtonTitles:#"OK", nil];
[alert addTextFieldWithValue:#"" label:#"Enter Name"];
[alert addTextFieldWithValue:#"http://" label:#"Enter URL"];
UITextField *tf = [alert textFieldAtIndex:0];
tf.clearButtonMode = UITextFieldViewModeWhileEditing;
tf.keyboardType = UIKeyboardTypeAlphabet;
tf.keyboardAppearance = UIKeyboardAppearanceAlert;
tf.autocapitalizationType = UITextAutocapitalizationTypeWords;
tf.autocorrectionType = UITextAutocorrectionTypeNo;
// URL field
tf = [alert textFieldAtIndex:1];
tf.clearButtonMode = UITextFieldViewModeWhileEditing; tf.keyboardType = UIKeyboardTypeURL;
tf.keyboardAppearance = UIKeyboardAppearanceAlert; tf.autocapitalizationType = UITextAutocapitalizationTypeNone; tf.autocorrectionType = UITextAutocorrectionTypeNo;
[alert show];
}
You can now create a UIAlertView with a style.
UIAlertView now has a property - alertViewStyle that you can set to one of the enum values
1. UIAlertViewStyleDefault
2. UIAlertViewStyleSecureTextInput
3. UIAlertViewStylePlainTextInput
4. UIAlertViewStyleLoginAndPasswordInput
Once you've created the alert view, you set it style and display it. After dismissing the alert, you can access the contents of the fields using the textFieldAtIndex property of the alert view.
or else you can use this one also.,
IAlertView* 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];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 70.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];
[nameField release];
Hope this information helps. Thank you.
Related
I am getting password in First alertView and if the password is incorrect, then another alertView shows that the password is incorrect. Now the first alertView disappears. I wanna to display the second alertView over the first alertView.
This is not So complicated work i just done like this:-
UITextField *forgetPass;
-(IBAction)Password{// here is a Action for login password
//[txtEmail resignFirstResponder];
UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:#"Enter your password"];
[dialog setMessage:#" "];
[dialog addButtonWithTitle:#"Cancel"];
[dialog addButtonWithTitle:#"OK"];
dialog.tag= 1;
forgetPass = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
forgetPass.delegate = self;
[forgetPass setSecureTextEntry:YES];
forgetPass.placeholder = #"password";
forgetPass.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
[forgetPass setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:forgetPass];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 0.0);
[dialog setTransform: moveUp];
[dialog show];
}
-(void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alert.tag==1)
{
if (buttonIndex == 1) { // OK pushed
if([forgetPass.text length]>0 )
{
}
else {
UIAlertView *alert2 = [[UIAlertView alloc]initWithTitle:#"Oops..!" message:#"please enter your current password" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
UITextField *forgetPass2 = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
forgetPass2.delegate = self;
[forgetPass2 setSecureTextEntry:YES];
forgetPass2.placeholder = #"password";
forgetPass2.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
[forgetPass2 setBackgroundColor:[UIColor whiteColor]];
[alert2 addSubview:forgetPass2];
forgetPass2.hidden=YES;
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 0.0);
[alert2 setTransform: moveUp];
alert2.tag=2;
[alert2 show];
[alert2 release];
}
}
if (buttonIndex == 0)
{
}
}
else if(alert.tag==2)
{
if (buttonIndex == 0)
{
[self Password];
}
}
}
i just create a demo for you might be its helps for please download this:-
http://www.sendspace.com/file/zptg8l
I'm using an AlertView to make a login/password screen. I have some problem to format the view correctly. I want to have a AlertView as high as that want (check printscreen if I'm not clear ) but I don't know how!
Here's the code I've made :
// Ask for Username and password.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Login et mot de passe" message:#"" delegate:self cancelButtonTitle:#"Annuler" otherButtonTitles:#"Sauvegarder", nil];
// Adds a username Field
textfieldName = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
textfieldName.placeholder = #"Login";
[textfieldName setBackgroundColor:[UIColor whiteColor]];
textfieldName.enablesReturnKeyAutomatically = YES;
[textfieldName setReturnKeyType:UIReturnKeyDone];
[textfieldName setDelegate:self];
[alertView addSubview:textfieldName];
// Adds a username
textfieldPassword = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 90.0, 260.0, 25.0)];
textfieldPassword.placeholder = #"Password";
[textfieldPassword setBackgroundColor:[UIColor whiteColor]];
textfieldPassword.enablesReturnKeyAutomatically = YES;
[textfieldPassword setReturnKeyType:UIReturnKeyDone];
[textfieldPassword setDelegate:self];
[alertView addSubview:textfieldPassword];
// Show alert on screen.
[alertView show];
[alertView release];
Alert view will adjust its contents (title and message) Try to add lines to the message:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Login et mot de passe"
message:#"\n\n\n\n\n" delegate:self cancelButtonTitle:#"Annuler"
otherButtonTitles:#"Sauvegarder", nil];
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.
With this code, I get this error:
'* -[NSMutableArray insertObject:atIndex:]: attempt to insert nil
object at 0'
categories=[[NSMutableArray alloc] init ];
UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:#"Category name"];
[dialog setMessage:#" "];
[dialog addButtonWithTitle:#"Cancel"];
[dialog addButtonWithTitle:#"OK"];
nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
[dialog show];
[dialog release];
[categories addObject:[nameField text]];
[nameField release];
[categoryTable reloadData];
When I run this in simulator, I get a crash before the alert view even pops up. Any ideas?
An exception will be raised in an NSArray if you try to add a nil object. Check for the condition first:
if ([nameField text]) [categories addObject:[nameField text]];
EDIT:
Also from your code, you need to implement the UIAlertViewDelegate protocol and attempt to add your object to your array there. For example:
- (void) showDialog {
UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:#"Category name"];
[dialog setMessage:#" "];
[dialog addButtonWithTitle:#"Cancel"];
[dialog addButtonWithTitle:#"OK"];
nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
[dialog show];
[dialog release];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if ([nameField text]) [categories addObject:[nameField text]];
[categoryTable reloadData];
}
Assumes nameField and categories are iVars and you will need to release them when they are no longer needed. You should also check which button was pressed in the delegate method. HTH Dave
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];