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.
Related
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];
}
I am looking to prompt the user to enter his/her name at the beginning of a game I am building.
What is the best way to get input from the user in cocos2d?
Thank you,
Joey
Cocos2d doesn't have any text input controls but you can easily add UIKit controls to the scene in Cocos2d 2.0
[[CCDirector sharedDirector] view] addSubview:myTextField];
You can use a UIAlertView with a text field embedded.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
//[alert release]; If not using ARC
To receive the events back from the UIAlertView you implement the UIAlertViewDelegate. In your header file add the delegate protocol to your interface
#interface BTMyScene : CCLayer <UIAlertViewDelegate>
Then in your implementation file add any of the methods from the delegate protocol you want to receive notifications for. You probably want this one
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
UITextField *textField = [alertView textFieldAtIndex:0];
NSString *name = textField.text;
}
I recommend reading the documentation for UIAlertView and UIAlertViewDelegate. You will see all the available methods that you can use.
I create a class to call UIAlertview show on my screen. I write the UIAlert function in another class. Both these two classes are not my viewController class.
I use this UIAlert, which is a UITextfield inside, to store texts into a plist file.
here is the class to call UIAlert:
#import "Story.h"
#implementation Story
...
+ (void)stage1
{
AlertClass *pointer = [AlertClass new];
[pointer doAlert];
}
here is the class AlertClass.m file:
- (void)doAlert
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
//this makes crash!
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
self.storyFlow.text = [alertView textFieldAtIndex:0].text;
}
Before I add UIAlertViewDelegate in the .h and override the method "clickedButtonAtIndex", it works great. However, I need to store some data from the UITextfield inside the alert view. I get crash and don't know the message it responds as following.
Please help me to solve this problem. Thanks.
[crash pic] https://dl.dropbox.com/u/47381923/crash.tiff
do an NSLog on the text you get back from the Alert View to see whether that is the crash or the subsequent 'self.storyFlow.text = ' is causing it. Perhaps self.storyFlow has not been created yet (with alloc/init)
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 UITextField added to an UIAlerView (Kind of adding user to list with nick name, and nickname is asked by the alertview.). Thats work perfect in iOS3, But I cant type to that UITextField in iOS4.1. in iOS4.1 I do get focus to that textfield inside alertview, and keyboard appears, however typing doesn't work.
Please help in this regard.
In iOS 4 UIAlertViews will automatically be moved and sized to avoid the keyboard if they contain any UITextFields in their subviews so you don't have to move it yourself. Simply add a text field like so:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hello"
message:#"Tap below to enter text:\n\n"
delegate:nil
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(12, 68, 260, 30)];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
[alert addSubview:textField];
[textField release];
[alert show];
[alert release];
Be sure to add \n's to make room for your text field, you'll have to play around with getting the field in the correct position.
iOS4 broke all sorts of UIAlertView hacks (er... 'customizations').
I have a custom UIAlertView replacement class on github which supports an input (with UITextField) mode. You're welcome to try it out.
https://github.com/TomSwift/TSAlertView
I have created a post in my blog on the topic "How to add UITextField to UIAlertView from XIB".
To Solve your problem, you need to add a "fake" UITextField into the Alert view via coding. Please refer to the following link:
http://creiapp.blogspot.com/2011/08/how-to-add-uitextfield-to-uialertview.html