I’m popping up a UIAlertView with a UITextField in it. I want the text field to auto-capitalize all words. I’m doing this by setting properties of the text field, but they have no effect at runtime. Here’s my code:
UIAlertView* alert = [[UIAlertView alloc] initWithTitle: title
message: message
delegate: self
cancelButtonTitle: #"Cancel"
otherButtonTitles: #"Create", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField* titleField = [alert textFieldAtIndex: 0];
titleField.autocapitalizationType = UITextAutocapitalizationTypeWords;
[alert show];
this autocapitalization is working on UITextField form XIB but this is not working on UIAlertView ,i am using iOS 6.
If it doesn't change your requirements, using
titleField.autocorrectionType = UITextAutocorrectionTypeDefault;
along with
titleField.autocapitalizationType = UITextAutocapitalizationTypeWords;
gives you the desired effect.
Make sure "Auto-Capitalization" is enabled in Settings in the iOS Simulator and/or on your test device.
Settings > General > Keyboard > Auto-Capitalization
If disabled, it will ignore the autocapitalizationType property on your UIAlertView's text field.
You can use uitextfield delegates
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(#"textFieldDidEndEditing");
textField.text=[textField.text uppercase];
}
Related
I have an application in which I'm using a specific design for a reason. I put a text field in an alert view above an otherbutton with a background image. Everything is working fine in ios 6 version.
UIAlertView *av=[[UIAlertView alloc] initWithTitle:#"fdhdj" message:#" hdfjkhfjkhdk" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:#" ",#"cancel",nil];
av.alertViewStyle = UIAlertViewStylePlainTextInput;
namefield = [[UITextField alloc] initWithFrame:CGRectMake(10.0,43.0, 264.0, 44.0)];
namefield.borderStyle = UITextBorderStyleNone;
namefield.background = [UIImage imageNamed:#"text_field_default.png"];
namefield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
namefield.textAlignment = UITextAlignmentCenter;
//[namefield setBackgroundColor:[UIColor whiteColor]];
[av addSubview:namefield];
[namefield release];
av.tag=12;
av.delegate=self;
[av show];
[av release];
But now in ios 7, I heard you can't easily alter the view hierarchy of a UIAlertView.
One alternative for this case is to set
alert.alertViewStyle = UIAlertViewStylePlainTextInput
But can we add that text field in wherever we want? As in my case above the first otherbutton.can anybody help me?
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Enter Student Name" message:#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Save",nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alertView show];
i used to do like this ,and its working very fine
This is my component to support addSubview with alertView in iOS7.
CXAlertView - Custom alert-view which allow you to add view as main content.
The simple answer to your question is NO, you can't change anything in this testField for the UIAlertViewStylePlainTextInput and you shouldn't.
This is from Apple:
The UIAlertView class is intended to be used as-is and does not
support subclassing. The view hierarchy for this class is private and
must not be modified.
And unfortunately what you heard I heard you can't easily alter the view hierarchy of a UIAlertView is wrong, you cannot alter the view hierarchy of a UIAlertView in iOS7 at all.
There are a good alternative on the web, you can check in cocoacontrols.com
You can't easily alter the view hierarchy of a UIAlertView in iOS 7. (Nor should you; the documentation specifically tells you not to.) Head over to the developer forums to see a long discussion about it.
One alternative in your case is to set alert.alertViewStyle = UIAlertViewStylePlainTextInput; This will add a text field for you. You can access it in the UIAlertView delegate callback by using UITextField *textField = [alertView textFieldAtIndex:0];.
I have an alert view with a uitextfield added as a subview.
In my uitableview controller the keyboard shows fine.
However in a different view I wanted to do the same thing. So instead of using a UITableView Controller I made it a UIViewController so that I could add a toolbar at the bottom of the view.
But when I display the UIAlertView the keyboard is hidden. I'm thinking it's behind the view because the alert view moves up to make room for the keyboard.
Any ideas?
UPDATE:
The reason the keyboard was being hidden was because I was dismissing a modalviewcontroller after showing the alert. For some reason it would dismiss the keyboard also I guess. Just rearranged the order an fit works fine now...
Try implementing the didPresentAlertView: method and inside set the text field to firstResponder like so:
- (IBAction)someActionThatTriggersAnAlertView:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"TITLE" message:#"MESSAGE" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Done", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *someTextField = [alert textFieldAtIndex:0];
someTextField.keyboardType = UIKeyboardTypeAlphabet;
someTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
someTextField.autocorrectionType = UITextAutocorrectionTypeNo;
[alert show];
[alert release];
}
#pragma mark - UIAlertViewDelegate Methods
- (void)didPresentAlertView:(UIAlertView *)alertView {
[[alertView textFieldAtIndex:0] becomeFirstResponder];
}
UIAlertView Documentation
UIAlertViewDelegate Documentation
I have a textField and a button. On the button I defined an alertview now I want what I write in textfield appear in alertview.
- (IBAction) btnClicked {
NSString *str=textTag.text;
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:#"Hello World!" message: str delegate: nil cancelButtonTitle:#"OK" otherButtonTitles: nil, nil];
[alert show];
[alert release];
}
The program is build successful but when I clicked on button nothing is displayed in the alertView.
What is the reason nothing is displayed?
What is textTag defined as? You need something such as this in your header file:
IBOutlet UITextField *textTag;
Not only that, you need to hook up textTag in interface builder to the actual text field. Have you done that?
To help diagnose the problem, add the following line below the NSString *str = ... line:
NSLog(#" Text I found in the text field = .%#.", textTag.text);
You should see your text field text appear in XCode console when you hit the button.
I'm really new at this. I've been working my way through a couple of "HelloWorld" type tutorials that basically have a text field, a button, and when you click the button it alerts whatever's in the text field. I've gotten as far as getting an alert to show and being able to set the title, message, and button text directly. However my attempts at getting the contents of my UITextField keep failing. I either get errors, no response, or just "(null)". My code's here.
#synthesize textField;
- (IBAction)btnClicked:(id)sender {
//text of textField
NSString *str = [[NSString alloc] initWithFormat:#"Hello, %#", textField.text];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hello!"
message:str delegate:self
cancelButtonTitle:#"Done"
otherButtonTitles:nil];
[alert show];
[alert autorelease];
}
It looks like the textField property is not properly linked to the UITextField object you have in interface builder. Make sure your IBOutlets are set correctly.
Is it possible for a UIPickerView to appear instead of a keyboard when a UITextField is selected? Every option in Interface Builder is some sort of keyboard.
I suppose I could create a UIPickerView programmatically and create one when the UITextField registers a touchUpInside event, then tell the UITextField to resignFirstResponder, but that seems a bit of a hack.
Is there an "official" or more "correct" way to do this?
Thanks!
You can implement this code and override the default behaviour (that is, showing the keyboard):
#pragma mark -
#pragma mark UITextFieldDelegate methods
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Bouh!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
return NO;
}
Instead of showing a UIAlertView, you might as well show your own UIPickerView and do your thing.