How to put uitextfield for user id and password in UIAlertView? - iphone

I am creating an application where i need to create two UITextField or TextBox for User id and password inside a UIAlertView for User login.I have tried something but here the textfield created becomes very big and almost cover the whole alert view.Please somebody help me how to organize the textfield and labels etc inside the alert view. I need like - there should be a small textfield against the label "User Id" and "Password". So how to do that? please help me. Thanx in advance.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"User Login" message:#"Please Enter the Following \n\n\n\n\n\n\n" delegate:self cancelButtonTitle:#"Close" otherButtonTitles:#"Sign In", nil];
UILabel *lblUserName = [[UILabel alloc] initWithFrame:CGRectMake(20, 70, 85, 21)];
lblUserName.tag =2;
//lblUserName.backgroundColor = [UIColor darkGrayColor];
lblUserName.font = [UIFont systemFontOfSize:15.0];
lblUserName.text = #"User Name:";
lblUserName.textColor=[UIColor whiteColor];
//[lblUserName setFont:[UIFont fontWithName:#"TimesNewRomanPS-boldMT" size:8]];
lblUserName.backgroundColor = [UIColor clearColor];
[alert addSubview:lblUserName];
UITextField *txtUserName;
//if(txtUserName==nil)
txtUserName = [[UITextField alloc] initWithFrame:CGRectMake(89, 50, 160, 30)];
txtUserName.tag = 3;
txtUserName.borderStyle = UITextBorderStyleBezel;
txtUserName.textColor = [UIColor whiteColor];
txtUserName.font = [UIFont systemFontOfSize:12.0];
txtUserName.placeholder = #"User ID";
[alert addSubview:txtUserName];
alert.tag=3;
[alert show];
[alert release];

As of iOS5, UIAlertView supports a login screen using the Alert Style UIAlertViewStyleLoginAndPasswordInput.
More information in the UIAlertView Class Reference

UIAlertView *alert =[UIAlertView alloc] init];
alert.delegate =self //make sure to add the UIAertViewDelegate in the .h file
alert.alertViewStyle =UIAlertViewStyleLoginAndPasswordInput;
alert.message =#"Please login";
[alert show];
[alert release];
Then in the clickedButtonAtIndex delegate method:
{
UITextField *username = [alertView textFieldAtIndex:0];
UITextfield *password = [alertView textFieldAtIndex:1];
NSLog(#"Username %"# password %"#,username.text, password.text);
}

Related

Keyboard does not open when display alertview with Textfield in ios 5.0

I have created AlertView with TextField using this code:
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:nil message:#"The person you selected does not have an email address on file. Please enter their email address below." delegate:self cancelButtonTitle:nil otherButtonTitles:#"Cancel",#"Submit",nil];
alert.tag = 1;
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeEmailAddress;
alertTextField.placeholder = #"Enter Email address";
[alert show];
It display fine and open keyboard in ios 6 or latter but not in ios 5.
Here is my device screenshot which run on ios 5.1
I have tried your code and its does not show keyboard in iOS 5 as you said but i have just added resignFirstResponder in your textField after alert show and its worked. Here is the code,
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:nil message:#"The person you selected does not have an email address on file. Please enter their email address below." delegate:self cancelButtonTitle:nil otherButtonTitles:#"Cancel",#"Submit",nil];
alert.tag = 1;
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
// assert(alertTextField);
alertTextField.keyboardType = UIKeyboardTypeEmailAddress;
alertTextField.placeholder = #"Enter Email address";
[alert show];
[alertTextField resignFirstResponder];
And if you don't want to resignFirstResponder in iOS 6 and later than put this code at the end of your code instead of resignFirstResponder code than textField only resignFirstResponder for iOS version less than 6.0
float currentVersion = 6.0;
if ([[[UIDevice currentDevice] systemVersion] floatValue] < currentVersion)
{
[alertTextField resignFirstResponder];
}
Along with
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeEmailAddress;
you also write
alertTextField.delegate = self;
And in your header file (i.e. .h), pass textfield delegate like <UITextFieldDelegate>
try with different keyboardTypes like UIKeyboardTypeDefault, UIKeyboardTypePhonePad, etc.
Also try [alertTextField setUserInteractionEnabled:YES];
alertTextField.delegate = self;
then in the delegate method,
-(void)textFieldDidBeginEditing:(UITextField *)textField {
NSLog(#"textFieldDidBeginEditing method was called");
if (!self.window.isKeyWindow) {
[self.window makeKeyAndVisible];
}
}
not pretty sure, but hope this works for you..
try this it is run on ios 5
CGRect textFieldFrame = CGRectMake(15.0f, 105.0f, 255.0f, 31.0f);
UITextField *txtYourTextField = [[UITextField alloc] initWithFrame:textFieldFrame];
txtYourTextField.placeholder = #"Your String";
txtYourTextField.backgroundColor = [UIColor whiteColor];
txtYourTextField.textColor = [UIColor blackColor];
txtYourTextField.font = [UIFont systemFontOfSize:14.0f];
txtYourTextField.borderStyle = UITextBorderStyleRoundedRect;
txtYourTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
txtYourTextField.returnKeyType = UIReturnKeyDefault;
txtYourTextField.keyboardType = UIKeyboardTypeEmailAddress;
txtYourTextField.delegate=self;
txtYourTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
txtYourTextField.tag = 1;
txtYourTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:#"The person you selected does not have an email address on file. Please enter their email address below.\n\n\n" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Send",nil];
alert.tag=1;
[alert addSubview:txtYourTextField];
[alert show];
Hope this helps you!!!

UITextView large text in UIAlertView

I have an UIAlertView with UITextView.
on ViewDidAppear I do [textView setText:] with a large text, but the alert shows an empty textView, and only after I touch the textView to scroll, the text appears.
What should I do in order to make the text appear in the textView in the alert, without scrolling it to "refresh" it?
Thanks!
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
av = [[UIAlertView alloc]initWithTitle:#"Terms of Service" message:#"\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:#"Disagree" otherButtonTitles:#"Agree",nil];
UITextView *myTextView = [[UITextView alloc] initWithFrame:CGRectMake(12, 50, 260, 142)];
[myTextView setTextAlignment:UITextAlignmentCenter];
[myTextView setEditable:NO];
myTextView.layer.borderWidth = 2.0f;
myTextView.layer.borderColor = [[UIColor darkGrayColor] CGColor];
myTextView.layer.cornerRadius = 13;
myTextView.clipsToBounds = YES ;
[myTextView setText:#"LONG LONG TEXT"];
[av addSubview:myTextView];
[myTextView release];
[av setTag:1];
[av show];
}
This is because you have initially set message as #"\n\n\n\n\n\n\n" for UIAlertView. Set your UITextView first and then set UIAlertView's message as textView's text.
Add \n characters in place of message in alert view . Then create a UILabel add add to alert view. Like this
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:#"%#\n\n\n", #"Title"] message:#"\n" delegate:self cancelButtonTitle:NEVER_DISPLAY_BUTTON_TEXT otherButtonTitles:nil];
[alert addButtonWithTitle:#"Text"];
[alert addButtonWithTitle:#"Text"];
[alert addButtonWithTitle:#"Text"];
[alert addButtonWithTitle:#"Text"];
[alert show];
newTitle = [[UILabel alloc] initWithFrame:CGRectMake(10,-55,252,230)];
newTitle.numberOfLines = 0;
newTitle.font = [UIFont systemFontOfSize:15];
newTitle.textAlignment = UITextAlignmentCenter;
newTitle.backgroundColor = [UIColor clearColor];
newTitle.textColor = [UIColor whiteColor];
newTitle.text = [NSString stringWithFormat:#"%#",self.message];
[alert addSubview:newTitle];
You might need to adjust size of Uilable to match in your alert view.
Try with this:
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"Terms of Service"
message:[NSString stringWithFormat:#"%# \n\n\n",myTextView.text]
delegate:self
cancelButtonTitle:#"Disagree"
otherButtonTitles:#"Agree",nil
];

No Keyboard with UIAlertViewStylePlainTextInput ?

I have a problem with the UIAlertViewStylePlainTextInput. I get my Alert with the textfield but there is no Keyboard coming up..? Here is my code:
UIAlertView *enter = [[UIAlertView alloc] initWithTitle:#"Highscores" message:#"Please Enter Your Name" delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:#"Abbrechen" nil];
enter.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *theTextField = [enter textFieldAtIndex:0];
theTextField.placeholder = #"Your Name";
theTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
theTextField.selected = NO;
[enter show];
I have tried without the
UITextField *theTextField = [enter textFieldAtIndex:0];
theTextField.placeholder = #"Your Name";
theTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
theTextField.selected = NO;
part but still there is no keyboard coming up.
(I've tested it on my device and on the simulator)
Do you have any ideas about that?
thank you in advance.
I had the problem because I was triggering the alert when a UITextField delegate method textFieldDidBeginEditing was called in my view and then I would fill this with the content of the Alert view text field. That meant that the UITextField in my view became first responder and I couldn't find a way to resign. So if you have the problem it is because something else is becoming first responder before the UIAlert is displayed. Think about how you trigger the alert.
I resigned to using a tap gesture on my UITextField which then triggers the UIAlertView with UIAlertViewStylePlainTextInput and it works fine.
Some code below:
-(void)addTextFieldToView{
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 31)];
textField.backgroundColor = [UIColor whiteColor];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.textAlignment = UITextAlignmentCenter;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showAlertWithTextField:)];
[textField addGestureRecognizer:tapGestureRecognizer];
[self.view addSubview:textField];
}
-(void)showAlertWithTextField:(UITapGestureRecognizer *)gesture{
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK",nil];
[dialog setAlertViewStyle:UIAlertViewStylePlainTextInput];
[dialog show];
}
Try below code:
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Your name?"
message:nil
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
[message show];
Works on iOS5
Might fix your problem as it solved mine:
[enter resignFirstResponder];

how to call a function as user click on search in alert view iphone

//---------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.

UIAlertView - retrieve textfield value from textfield added via code

Here is the code I have to create an UIAlertView with a textbox.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter A Username Here" message:#"this gets covered!"
delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:#"OK!", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);
[alert setTransform:myTransform];
alert.tag = kAlertSaveScore;
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
[alert release];
[myTextField release];
My question is, how do I get the value from the textfield in:
- (void) alertView:(UIAlertView *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
}
I know I can get the standard stuff for the alertview such as actionSheet.tag and such, but how would I get the above created textfield?
#interface MyClass {
UITextField *alertTextField;
}
#end
And instead of declaring it locally, just use that.
//...
alertTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
//...
- (void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *text = alertTextField.text;
alertTextField = nil;
}
Just give it a tag, and find it using the tag later. So, using your code:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter A Username Here" message:#"this gets covered!"
delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:#"OK!", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);
[alert setTransform:myTransform];
alert.tag = kAlertSaveScore;
// Give the text field some unique tag
[myTextField setTag:10250];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
[alert release];
[myTextField release];
Then, in the call-back, wherever that happens to be and without having to worry about memory management or state management of the text field:
- (void) alertView:(UIAlertView *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Get the field you added to the alert view earlier (you should also
// probably validate that this field is there and that it is a UITextField but...)
UITextField* myField = (UITextField*)[actionSheet viewWithTag:10250];
NSLog(#"Entered text: %#", [myField text]);
}