Change type of UIAlertView keyboard - ios

How do I specify that my keyboard for the UIAlertView text field is to be a number pad keyboard? I am setting alertViewStyle to UIAlertViewStylePlainTextInput.

I think you can do something like:
UITextField* tf = [alertView textFieldAtIndex:0];
tf.keyboardType = UIKeyboardTypeNumberPad;
See the UIAlertView and UITextInputTraits docs

Related

Programmatically focus on UITextView that is created as an AccessoryView

In my iOS app, I create a UIAlert and add a UITextView to it dynamically:
UIAlertView *tweetAlert = [[UIAlertView alloc] initWithTitle:#"Compose Tweet"
message:nil
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
tweetTextView = [UITextView new];
[tweetTextView setText:[NSString stringWithFormat:#"%# %#", [[NSUserDefaults standardUserDefaults] valueForKey:#"tweetText"], self.setShareURL]];
[tweetAlert setValue:tweetTextView forKey:#"accessoryView"];
[tweetAlert addButtonWithTitle:#"Tweet"];
[tweetAlert show];
[tweetTextView becomeFirstResponder]; // focus textview and open keyboard
I attempt to focus on the UITextView in the last line so that the keyboard is open when the UIAlertView appears. However, the keyboard does not appear, and the UITextView is not in focus.
I also tried
[[tweetAlert valueForKey:#"accessoryView"] becomeFirstResponder];
But I get the error
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIAlertView 0x1358b3200> valueForUndefinedKey:]: this class is not key value coding-compliant for the key accessoryView.'
Any idea how to focus on the dynamically created UITextView, which is embedded as an accessoryView in a UIAlertView?
You can use the UIAlertViewDelegate method - (void)didPresentAlertView:(UIAlertView *)alertView to make the textview become first responder.
- (void)didPresentAlertView:(UIAlertView *)alertView {
[tweetTextView becomeFirstResponder];
}
It is generally not recommended that you modify the view hierarchy of alert views. You should just use a text entry UIAlertView:
UIAlertView *tweetAlert = [[UIAlertView alloc] initWithTitle:#"Compose Tweet"
message:nil
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
tweetAlert.alertViewStyle = UIAlertViewStylePlainTextInput;

UIAlertView textfield not accepting other language characters

In my iPad application, there is an alertview of following style UIAlertViewStylePlainTextInput. The problem is I'm unable to enter any other language characters(å,ß,Ω) in alertview textfield. It's simply showing blank string in textfield after entering other language character.
saveAlert = [[UIAlertView alloc] initWithTitle:#"Please provide a name for your design." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK",#"Cancel",nil];
[saveAlert setTag:SAVE_HTML_NAME_ALERT_TAG];
[saveAlert setAlertViewStyle:UIAlertViewStylePlainTextInput];
[saveAlert show];
alertTextField = [saveAlert textFieldAtIndex:0];
alertTextField.font = [UIFont fontWithName:#"Arial" size:15.0f];
[alertTextField setTag:SAVE_ALERT_TEXT_FIELD_TAG];
[alertTextField setDelegate:self];
Other normal textfields are working fine in application, properly displaying other language characters. There's only problem with alertview's textfield.

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.

Accessibility: On updating UIAlertView's message, the text that Voice over reads does not change?

Please refer the code below:
UIAlertView *progressAlertView = [[UIAlertView alloc]
initWithTitle:#"Title"
message:#"Message"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
progressAlertView.message=#"Hello, I am the new one";
Voice over always reads "Message" and never reads the new message string set in the second line "Hello, I am the new one". There's no way to change this accessibility label of the bodyTextLabel control/subView of UIAlertView.
Any idea how to make UIAlertView change its accessibility labels after its alloc'ed?
Thanks,
-Shilpi
You can use accessibilityTraits as UIAccessibilityTraitUpdatesFrequently to read updated message.
it works like:
alertObj.accessibilityTraits = UIAccessibilityTraitUpdatesFrequently;
Thanks
use this way for set message of UIAlertView
UIAlertView *progressAlertView = [[UIAlertView alloc]
initWithTitle:#"Title"
message:#"Message"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
[alertView setMessage:#"Hello, I am the new one"];
[alertView show];
For More About UIAlertView Refer this
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html
You can change the subviews of UIAlertView to change single attributes like the accessibility label:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Test"
message:#"Message"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
// In a standard UIAlertView there are 2 UILabels and some buttons
// The UILabel with the message is always the 2nd object in the array
NSArray *subviews = alert.subviews;
UILabel *messageLabel = [subviews objectAtIndex:1];
[messageLabel setAccessibilityLabel:#"Some other text"];
With this code you only change the accessibility label, so that VoiceOver reads another text but the old text is shown.
In your case you should set the accessibility label to the same as you want to set the message of the UIAlertView.

How to retrieve text from UIAlertViewStylePlainTextInput

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.

Resources