In order to satisfy the need of my leader, I have to pop up multiple alertViews (of style UIAlertViewStyleSecureTextInput when a signal come).
But I am encountering a strange error... After multiple alertView popups, I lose the focus of another textField.
the following code:
if (!didDisplayAlarm && (timeInterval < -_alertStopTime) && (self.isMainViewController)) {
didDisplayAlarm = YES; //first method
if (_alertView) {
[_alertView dismissWithClickedButtonIndex:0 animated:NO]; //second method
}
_alertView = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:#"ignore"
otherButtonTitles:#"check", nil];
_alertView.tag = kAlarmTag;
alarmUser = [NSString stringWithFormat:#"%d", [[dict objectForKey:#"alarmUser"] intValue]];
alarmPassword = nil;
_alertView.alertViewStyle = UIAlertViewStyleSecureTextInput;
UITextField *alertTextField = [_alertView textFieldAtIndex:0];
alertTextField.keyboardAppearance = UIKeyboardAppearanceDefault;
alertTextField.keyboardType = UIKeyboardTypeNumberPad;
[alertTextField setPlaceholder:#"please input password"];
[_alertView show];
}
I tried three method to avoid such issue:
set a bool var didDisplayAlarm to reduce the popup of alertView
use dismiss method of alertView
use textfield resignFirstResponder in the alertView delegate
My environment is IOS 6.1.3
but all doesn't work.
The problem here is you are using resignFirstResponder but your textfield is not identifying which one to resign. So when you are resigning the textfield then you have to make other textfield to be first responder. For example,lets say if you have multiple alertView and each contains separate textfield. So for determining the textfield just set the tag Value on textfield and on the basis of tag value resign the textfield and make other one textfield to be the first responder.
When you click ok/cancel in alertview you need to resign your keyboard.
to get UITextField from alertview in clickedButtonAtIndex: delegate method we have two options
Option 1 - loop the subviews of alert and get the instance of textfiled and resign it.
for (UIView* view in alertView.subviews)
{
if ([view isKindOfClass:[UITextField class]])
{
UITextField* textField = (UITextField*)view;
NSLog(#"text:[%#]", textField.text);
break;
}
}
Option 2 - you can get the textfiled instance by [alertView textFieldAtIndex:0].text. I think this can be done only if the alert style is UIAlertViewStylePlainTextInput.
Related
I have an application that uses UIAlertView in its login Window normally:
self.customAlert = [[IFCustomAlertView alloc] initWithTitle:#"Save Password"
message:#"¿Do you want the app to remember your password?"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:#"Cancel", nil];
The problem is... Since I updated my devices to iOS8 whenever this alertView comes up it shows the keyboard and I can't dismiss it. On iOS7 this doesn't happen.
I am resigning the responders of user and password when the send button is tapped:
-(IBAction)btnSendTapped:(id)sender{
[self.tfpass resignFirstResponder];
[self.tfuser resignFirstResponder];
}
I have tried:
[self.view endEditing:YES];
and in some alertViews it does work but in others it doesn't. My AlertViews never have text fields so I think there's no reason for this keyboard to appear.
Also the intro button on the keyboard doesn't hide it, so sometimes the OK and Cancel buttons are obstructed by the keyboard and I can't do nothing on the screen.
I think this may have something to to with the UIAlertView deprecation, but I don't know.
I also have these methods implemented:
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return true;
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}
Any help would be appreciated.
I borrow solution from this blog
For me the keyboard always show up when the alertView.show() has been called.
My solution is using the didPresentALertView method for make sure this will called after the alert view popup. Then we can loop through all UIWindows and it's subviews. I detect it by description name (You can use more accurate method if you want) and just simply remove it from superview.
func didPresentAlertView(alertView: UIAlertView) {
var tempWindow: UIWindow;
var keyboard: UIView;
for var c = 0; c < UIApplication.sharedApplication().windows.count; c++ {
tempWindow = UIApplication.sharedApplication().windows[c] as! UIWindow
for var i = 0; i < tempWindow.subviews.count; i++ {
keyboard = tempWindow.subviews[i] as! UIView
println(keyboard.description)
if keyboard.description.hasPrefix("<UIInputSetContainerView") {
keyboard.removeFromSuperview()
}
}
}
}
Hope this thelp.
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.
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).
I'm calling a UIAlertView with the PlainTextView style, and I'm trying to work out how I can get access to whatever the user entered in the text view before they clicked OK.
I'm using willDismissWithButtonIndex and the alertView doesn't seem to have any properties like text, or anything.
How can I get at it? Thanks in advance!
Get the text field with
UITextField *textfield = [alertView textFieldAtIndex: 0];
In your delegate method.
Refer to the documentation for further information.
Edit march 2015 due to many views the swift equivalent :
let textfield = alertView.textFieldAtIndex(0)
UITextField *textfield = [alertView textFieldAtIndex: 0];
Use this in uialertview delegate
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField *textfield = [alert_name textFieldAtIndex: 0];
}
Use its method:
- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex
For index 0.