How to retrieve text from UIAlertViewStylePlainTextInput - ios

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.

Related

UITextField cannot get keyboard when mulple alertView popup?

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.

How to access UIButton in UIAlertView

I'd like to selectively enable the 'save' button on my UIAlertView so that you can't save a file with no name. I can listen to text changed events for the attached UITextView (the style is UIAlertViewStylePlainText), but I can't see how to access the buttons so that I can do the enable/disable. I've tried iterating through [alertView subviews], but there is only the label in there (no buttons). Where do I need to look to directly access the UIButtons attached to a UIAlertView?
Simple, just implement the UIAlertViewDelegate in your class and utilize the alertViewShouldEnableFirstOtherButton: delegate method. You can use this to check the length of the text field, and enable the button accordingly...
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
return [[[alertView textFieldAtIndex:0] text] length] > 0;
}
Make sure you set your view controller to conform to this delegate in your interface using < UIAlertViewDelegate > and to set this class as the alerts delegate upon instantiation.
Instantiate your alertview:
UIAlertView *av = [UIAlertView alloc] initWithTitle:#"my title" message:#"message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Save", nil];
});
Then make sure your class implements UIAlertViewDelegate, and look for the button index of the button you want to listen to:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex==1) {
//save here
}
}
0x7fffffff posted right answer, but if someone is looking for answer how to add that textfield and save button to UIAlertView, here is the answer:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Save" message:#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Save", nil];
[alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
This delegate method:
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView {
return [[[alertView textFieldAtIndex:0] text] length] > 0;
}
won't work if you add that save button with addButtonWithTitle: method.
And when save button is clicked, this delegate method is called and here you can read what user wrote to that text field:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 1) {
// Save button clicked
NSLog(#"%#", [[alertView textFieldAtIndex:0] text]);
}
}
You can't access the buttons of an alert view as of iOS 7. Unfortunately they made the subviews completely hidden and there's no way to access them.

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.

How to move focus on to a Textfield after an alertView from another textField

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

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