Don't dismiss UIAlertView (without subclassing) - ios

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

Related

Make UITextField's keyboard reappear if IBAction returns an error

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

How do I force a button click on a UIAlertView?

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
}
}

How do I add `accessibilityLabel` to `UIAlertView` buttons?

How do I add accessibilityLabel to UIAlertView buttons?
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Announcement"
message: #"message!"
delegate: nil
cancelButtonTitle: #"cancelButton"
otherButtonTitles: #"otherButton"];
[alert show];
According to Apple's documentation (search for 'Making Alert Views Accessible'), AlertViews are 'accessible by default'. This, and the fact that the buttons aren't editable, means that you probably shouldn't try changing the accessibilityLabels yourself. By default they use the button's title and the word 'button', which should be fine.
Accessibility for alert views pertains to the alert title, alert message, and button titles. If VoiceOver is activated, it speaks the word “alert” when an alert is shown, then speaks its title followed by its message if set. As the user taps a button, VoiceOver speaks its title and the word “button.” As the user taps a text field, VoiceOver speaks its value and “text field” or “secure text field.”
The only way you can do this is finding the UIButtons within the UIAlertView subviews:
for (id button in self.alertView.subviews){
if ([button isKindOfClass:[UIButton class]]){
((UIButton *)button).accessibilityLabel = #"your custom text";
}
}
However this is the only way to do it because there is not a public API to access these UIButtons and this is because Apple doesn't want you to access them. Accessing internal views of the UIAlertView class is something which Apple does not allow and it is likely that will make your app rejected during the App Store review process.
If you really need to have UIButtons with a custom accessibilityLabel you should look into designing a custom alert view instead of using the Apple UIAlertView class.
Late, but maybe useful to someone.
You can get access to the alert buttons using their index. For instance you can find the second button in the screen like the following way in Objective C:
- (void)getAlertActionButton:(XCUIApplication *)app {
// any code
XCUIElement *secondAlertButton = [[[app.alerts otherElements] buttons] elementBoundByIndex:1];
// any code
}
To find your alert window hierarchy use debug mode.

iOS UIAlertview clear text and disable OK

I'm a bit new to iOS development, and right now am working on some simple UI-related stuff. I have a UIAlertView that I'm using at one point to allow the user to enter some text, with simple Cancel and OK buttons. The OK button should be disabled if the text field is blank.
I added to my UIAlertViewDelegate an alertViewShouldEnableFirstOtherButton function, so the OK button would disable when there's no text, and I also set the UIAlertView's UITextField to have clearOnBeginEditing true, so the previous text would be gone every time I displayed the alert. Each of these things works perfectly on their own. Unfortunately, it seems like the AlertView is checking whether or not to enable the OK button before the text field is cleared, so when they're put together it comes up enabled. Below should be about the minimal code needed to reproduce.
-(void)viewDidLoad
{
textEntryBox = [[UIAlertView alloc] initWithTitle:#"Name" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[textEntryBox setAlertViewStyle:UIAlertViewStylePlainTextInput];
[textEntryBox textFieldAtIndex:0].clearsOnBeginEditing = YES;
}
-(IBAction)functionTriggeredByOtherLogic
{
[textEntryBox show];
}
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
if(alertView == textEntryBox)
{
if([[alertView textFieldAtIndex:0].text length] > 0)
{
return YES;
}
else
{
return NO;
}
}
return YES;
}
So, ultimately, my question is this: am I doing something completely against the natural iOS way of doing things here? Is there a better way to do this? Should I just ignore the clearsOnBeginEditing property of the UITextField, and manually clear the Text property before showing the UIAlertView?
Try to set the textfield delegate to self
[[textEntryBox textFieldAtIndex:0] setDelegate:self]
and implement this method :
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField setText:#""];
}
I'm also having a UIAlertView with a textField to fill-in in my app, and it works for me
Using an alert view for this is probably a bit much. It might be easier if you use the master-detail paradigm and just push a new view controller where you can enter your values.

Calling an IBaction in a alertView if statement

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

Resources