In an iOS application that I am coding I am displaying a UIAlertView to ask the user for a password and I am trying to allow them to hit enter in the text field to submit the password.
I have handled the enter from the text field in a textFieldShouldReturn function. The problem is in the examples I have found online which say to use the dismissWithClickedButton method to handle the button click.
When I try this it simply dismisses the UIAlertView without checking the password. I really need the "OK" button I have setup to fire so I check the password.
Any ideas on how to do this?
You can use delegate method of UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
Buttons in UIAlertView is stored in an array. So you can find the position of OK button
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1) //Your OK button
{
//Do Stuff
}
}
Related
I am a beginner in iOS development, and yesterday I learned about the UIAlertViewDelegate protocol from the Apple developer website.
I used -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex for managing the buttonindex and the performing the specific task.
However, when I use 2 UIAlertViews on the same outlet, they both use the same UIAlertViewDelegate protocol. How can I use different delegates for the other UIAlertView in my application?
Also, how can I change the default design of the UIAlertView in my Application?
Your UIAlertView has a property called tag. After you instantiate your alert view, set its tag:
myAlertView.tag = 1;
Use a different tag value for each of your alert views.
In your call back method, check the tag first to determine which alert view was dismissed and handle accordingly:
if (alertView.tag == 1) {
//alert view 1 was dimissed, handle that
} else if (alertView.tag == 2) {
//alert view 2 was dismissed
}
I have a login view controller with 2 text fields for username and password entry.
Once the user is done entering their password in the password text field, they press the "Done" return key on the keyboard and my IBAction is performed and tries to log them in.
However, if the login is unsuccessful I show them a UIAlertView warning that tells them it failed and then they press "OK" to dismiss the alert view.
I would like to make is to that when they press "OK" to dismiss the UIAlertView, the password text field's keyboard will pop up again / reappear so they can try entering in their password again.
Is there a way to programmatically call a certain text field's keyboard to popup?
Thanks.
EDIT: The keyboard is now successfully popping up using this code:
[_passwordEntry becomeFirstResponder];
but I would like to make it so that it only pops up when the user taps the "OK" button on the UIAlertView.
Is there a way to "listen" for the UIAlertView's "cancelButtonTitle" or dismissal and call my keyboard method when that happens?
Just got it working with the following code:
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//u need to change 0 to other value(,1,2,3) if u have more buttons.then u can check which button was pressed.
if (buttonIndex == 0) {
[_passwordEntry becomeFirstResponder];
}
}
Taken from this SO Answer: https://stackoverflow.com/a/6875248/3344977
I am using a UIAlertView to prompt users for a password. If the password is correct, I'd like the alert view to go away normally; if the password is incorrect, I'd like the alert view to remain (with the text changed to something like "Please try again."). How do I alter the default behaviour so that the view doesn't go away when a button is pressed?
I've tried the subclassing method suggested at Is it possible to NOT dismiss a UIAlertView, but the dismissWithClickedButtonIndex wasn't being called in my subclass. After reading the documentation I see that subclassing UIAlertView isn't supported, so I'd like to try another approach.
If this isn't possible with UIAlertView, is the best option to simply make a password view from scratch? Thanks for reading.
As this is very old question,but I got one solution and though of posting if any other developer need in near future.
Implement protocol methods in .h file
In order to respond to button taps in our UIAlertView, we will use the – alertView:clickedButtonAtIndex: protocol method as
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
}
//Now below code will check if uitextfield value.
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
NSString *inputText = [[alertView textFieldAtIndex:0] text];
if( [inputText length] > 0)
{
//text field value is greater than zero ,then Done button will appear as blue,or else it will be blurred
return YES;
}
else
{
return NO;
}
}
You could design a UIView similar to the alertview and can cal the functions inside this. Removing a UIView is controlled by you.
It's not possible to leave the alert there when a button has been clicked.
You can either disable the "OK" button by delegate method alertViewShouldEnableFirstOtherButton:, if you are checking the password locally, or just show another alert later. The second alert can contain a different information, e.g. "Password incorrect" instead of "Enter password".
In my add contact page, am validating my email textfield , phone number fields, etc. And am checking valid email on clicking all other textfields. So when i click on the phone number field, it will check email validity and show an alertview if email is invalid. So when i click ok button in the alertView, the cursor should go to the email textfield instead of being in the phone number field.. Can anyone help on this..?
for making any textfield first responder. and you can do this in your alertview delegate.
[yourTxtField becomeFirstResponder];
in alertview's delegate method,
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if([alertView isEqual:emailAlert])//emailAlert should be the instance variable
{
if(buttonIndex == 0)//assumes ok button index is 0
{
[textfield becomeFirstResponder];
}
}
}
Make your view controller implement the UIAlertViewDelegate, and add the current class as delegate to the shown alert view. In – alertView:clickedButtonAtIndex: make phone text field resign first responder and make the email text field befome first responder.
You can set AlertView's delegate and on click of "OK" button ,
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
[txtEmail becomeFirstResponder]; // your "Email" textField will get focused
}
}
Try this way.. HTH :)
Make sure that you class conforms to UIAlertViewDelegate protocol and show the alert like
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Your Message" delegate:self cancelButtonTitle:#"cancel " otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
implement the delegate method like
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0)//ok button index is 0
[textFieldEmail becomeFirstResponder];
}
Me and my buddy are working on an app, we're total newbies but have come a long way with books and goggling.
We're stuck now on this thing. We have a bunch of texfields that we have clear button linked to it with this action, but then we want that action to be called if you click "Yes" on one of the alert view buttons.
- (IBAction)clearText:(id)sender {
Spelare1Slag1.text = #"";
Spelare1Slag2.text = #"";
}
We also have this alert view:
alertDialog = [[UIAlertView alloc]
initWithTitle: #"Warning"
message: #"Do you want to delete?"
delegate: self
cancelButtonTitle: #"No"
otherButtonTitles: #"Yes", nil];
- (void)alertView: (UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"No"]) {
}
else if ([buttonTitle isEqualToString:#"Yes"]){
Spelare1Slag1.text = #"";
}
}
So this is how we think we should do it, but we don't know what to put in the else if statement. We want the textfields to clear when you press the "yes" button in the alert view, and not when you press "no"
Thanks in advance!
The clearText method, I'm assuming, is a custom method you created to delete the text in both the fields right? So instead of it being an IBAction, it should be a void method :
- (void)clearText {
Spelare1Slag1.text = #"";
Spelare1Slag2.text = #"";
}
Now all you need to do in your UIAlertView delegate method, is call the clearText method :
- (void)alertView: (UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Yes"]){
[self clearText];
}
}
Hope this helps
You could dismiss the AlertView in case of user has clicked NO If I understand question properly.
You could dismiss the alertview like this
[alertView dismissWithClickedButtonIndex:0 animated:YES];
but make sure to see if NO has index 0 or 1, if you are not sure then just do like this
[alertView dismissWithClickedButtonIndex:nil animated:YES];
Methods of type IBAction are just like any other method, and you can call them in your code directly. In fact, IBAction is simply a macro evaluating to void. Usually, you'll pass a nil sender argument when it needs to be called outside of the context of a target / action event being triggered.
[self clearText:nil];
Why do you need to check the actual button text? Checking the button index is the most efficient way to go. If you have two fields in your alertView, just check if the index is 0 or 1 and your good to go. Your doing extra work checking the actual text.
And btw, just do a check for the index of the YES button if you don't need to do anything specific when they press no. (Don't check both indexes if you don't need to).