I've searched through this site that I couldn't find a solution to the problem I'm facing now. Hope someone can help.
I've created a UIAlertView to prompt user to enter their name in an iPhone app.
UIAlertView *enterNameAlert = [[UIAlertView alloc] initWithTitle:#"Enter your name"
message:#"\n\n\n"
delegate:self
cancelButtonTitle:NSLocalizedString(#"Cancel", nil)
otherButtonTitles:NSLocalizedString(#"OK", nil),nil];
UITextField *enterNameField = [[UITextField alloc] initWithFrame:CGRectMake(16, 83, 252, 25)];
enterNameField.keyboardAppearance = UIKeyboardAppearanceAlert;
enterNameField.borderStyle = UITextBorderStyleRoundedRect;
enterNameField.autocorrectionType = UITextAutocorrectionTypeNo;
enterNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
enterNameField.returnKeyType = UIReturnKeyDone;
enterNameField.delegate = self;
[enterNameField becomeFirstResponder];
[enterNameAlert addSubview:enterNameField];
[enterNameAlert show];
[enterNameAlert release];
[enterNameField release];
I've setup this viewController to comply with UITextFieldDelegate in the header file, and implemented textFieldShouldReturn: trying to dismiss the keyboard when user hit Done.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
NSLog(#"text field was first responder");
} else {
[textField becomeFirstResponder];
[textField resignFirstResponder];
NSLog(#"text field was not first responder");
}
NSLog(#"textfieldshouldreturn");
return YES;
}
In the debugger, I confirm that this method is called successfully when I tap on the Done button, with the textField being the first responder at that time. However, the keyboard doesn't go away, but the little clearButton goes away. It is clear that the textField is no longer the first responder because when I click the Done button again, nothing get called. I just want to dismiss the keyboard with the Done button. Can anyone please offer a solution? Million thanks.
First you need to define your enterNameAlert in interface header. then use the below code.
- (void)viewDidLoad {
[super viewDidLoad];
enterNameAlert = [[UIAlertView alloc] initWithTitle:#"Enter your name"
message:#"\n\n\n"
delegate:self
cancelButtonTitle:NSLocalizedString(#"Cancel", nil)
otherButtonTitles:NSLocalizedString(#"OK", nil),nil];
UITextField *enterNameField = [[UITextField alloc] initWithFrame:CGRectMake(16, 83, 252, 25)];
enterNameField.keyboardAppearance = UIKeyboardAppearanceAlert;
enterNameField.borderStyle = UITextBorderStyleRoundedRect;
enterNameField.autocorrectionType = UITextAutocorrectionTypeNo;
enterNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
enterNameField.returnKeyType = UIReturnKeyDone;
enterNameField.delegate = self;
[enterNameAlert addSubview:enterNameField];
[enterNameField becomeFirstResponder];
[enterNameAlert show];
[enterNameField release];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
[enterNameAlert dismissWithClickedButtonIndex:1 animated:YES];
[enterNameAlert release];
NSLog(#"text field was first responder");
} else {
[textField becomeFirstResponder];
[textField resignFirstResponder];
NSLog(#"text field was not first responder");
}
NSLog(#"textfieldshouldreturn");
return YES;
}
What happens if you move the releasing of the text field to the delegate method, after you call the resignFirstResponder method?
Try dismissing keyboard using resignFirstResponder on "DidEndOnExit" event of your TextField.
Then try to dismiss keyboard by pressing Done button on keyboard when you have finished entering the data into your TextField.
Hope this helps.
Have another object become and then immediately resign the first responder.
simply do
[mytextField addTarget:self
action:#selector(methodToFire:)
forControlEvents:UIControlEventEditingDidEndOnExit];
if anyone is having trouble with this in storyboard i had to go into my uitextfields properties and ctrl+drag from its delegate to its root view controller all in storyboard and that fixed everything..
Related
I've added UITextView as a sub view of UIAlertView, I've added UIToolBar with a Done button (using UIBarButtonItem) as inputAccessoryView to allow user to resign keyboard`. It's just working fine in iOS6.0. And not in iOS5.0.
I've break point and checked in all the way I can, for reconfirming my self, I've made a sample project and checked the same in both the iOS versions, and the problem is same.
Here's the code, that's messing with me,
-(UIToolbar *)accessoryView
{
if (!accessoryView) {
accessoryView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0)];
UIBarButtonItem *btn = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(hideKeyBoard)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
accessoryView.items = [NSArray arrayWithObjects:flexibleSpace,btn, nil];
[accessoryView setTintColor:[[UIColor blackColor]colorWithAlphaComponent:0.5]];
}
return accessoryView;
}
-(IBAction) showAlertWithTextView: (id) sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithFrame:CGRectMake(0, 50, 320, 200)];
alert.title = nil;
alert.message = nil;
alert.delegate = self;
//textview I've added in .h file
textView = [[UITextView alloc] initWithFrame:alert.bounds];
textView.text = #"This is a UITextView";
textView.keyboardAppearance = UIKeyboardAppearanceAlert;
textView.editable = YES;
textView.inputAccessoryView = [self accessoryView];
[alert addSubview:textView];
[alert show];
}
- (void)hideKeyBoard {
[textview resignFirstResponder];
//[self.view endEditing:YES]; //this is also not worked
}
Here's list of steps I'm doing,
Showing Alert with Textview
Focus to Textview
Tap on Done button to resign TextView
Its not resigning in iOS5 but resigning in iOS6
Any idea? What's going wrong?
Actually your textview is not the first responder. The first responder is your alertView.
So you should try this....
[textField resignFirstResponder];
[alertView resignFirstResponder];
To use this code you must declare your alertView object in your.h file
I've solved it, as solution given by #iOS Developer, but as I've so many UIAlertView objects to handle, I've added a property of UIAlertView in delegate, and assigning it with the object of my alertview like this,
-(void)willPresentAlertView:(UIAlertView *)alertView
{
//set current alertview
[[AppDelegate sharedDelegate] setCurrentAlertView:alertView];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//will remove current alertview reference
[[AppDelegate sharedDelegate] setNilCurrentAlertView];
}
- (void)hideKeyBoard {
[textView resignFirstResponder];
//when need to resign UITextView, get current alertview object
UIAlertView *alertView = [[AppDelegate sharedDelegate] getCurrentAlertView];
if(alertView) {
[alertView resignFirstResponder];
}
}
In AppDelegate.m file,
#pragma mark - UIAlertView Resign
- (void) setCurrentAlertView:(UIAlertView *)alert{
self.alertObj = alert;
}
- (void) setNilCurrentAlertView {
self.alertObj = nil;
}
- (UIAlertView *)getCurrentAlertView {
return (UIAlertView *)self.alertObj;
}
As it is somewhat late but I am wondering that your code will not work in iOS7 or not as from iOS7 onwards UIAlertView is not allowing to add any subview (As you are adding UITextView as subview of UIAlertView).
If your requirement is not to compulsory using the UITextView then you can use UIAlertView with following configuration with default textfield in alert.
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Message"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
[message show];
And you can get value of this textfield in delegate of UIAlertView like this,
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
[alertView textFieldAtIndex:0].text;
}
Keyboard not resigning second time on the same textfield. I used UITextFieldDelegate. For Ex: I enter something in Name textField and I resign the keyboard. I clicked in MobileNo field entered something. This time keyboard not resigning.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Make delegate of your MobileNo
MobileNo.delegate=self;
Or if your are using xib then make Outlet connection of delegate
Try this,
- (void)viewDidLoad
{
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
}
-(void)dismissKeyboard
{
[self.view endEditing:YES];
}
I've added my UITextField and UITextView inside a UIWindow I can not able to type inside in both of them. Both are not letting me allow to type in those fields. And, can't able to dismiss it when return pressed in keyboard.
-(void)showAlert
{
alertWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
alertView = [[UIView alloc] initWithFrame:CGRectMake(10, 115, 300, 230)];
alertWindow.windowLevel = UIWindowLevelAlert; // puts it above the status bar
alertView.center = CGPointMake(alertWindow.frame.size.width/2, alertWindow.frame.size.height/2);
alertView.backgroundColor = [UIColor whiteColor];
alertView.layer.borderWidth = 1.0;
alertView.layer.borderColor = [[UIColor whiteColor]CGColor];
UITextField *txt = ....
....
....
txt.delegate = self;
...
...
[alertView addSubview:txt];
UITextView *txtview = ...
....
....
txtview.delegate = self;
...
[alertView addSubview:txtview];
[alertWindow addSubview:alertView];
[alertWindow addSubviewWithZoomInAnimation:alertView duration:1.0 option:curveValues[0]];
[alertWindow setHidden:NO];
}
I've declared both of the delegate methods also. I placed a breakpoint there and checked also, even that method not yet called.
Update -
textFieldShouldReturn delegate method
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
[reserveView resignFirstResponder];
[reserveWindow resignFirstResponder];
[self.reserveWindow endEditing:YES];
[reserveView endEditing:YES];
return YES;
}
You should try this for dismiss the keyboard from the UIWindow
[self.window endEditing:YES];
Source
I found, that method has not been called because of UIWindow So, i changed my UIWindow to UIView And, i fixed this issue by using UIView+Animation.h from here It provides the animation what i required exactly.
- (void) addSubviewWithZoomInAnimation:(UIView*)view duration:(float)secs option:(UIViewAnimationOptions)option;
- (void) removeWithZoomOutAnimation:(float)secs option:(UIViewAnimationOptions)option;
Thanks to Google! Cheers!
The keyboard will never dismiss on its own (even if you hit return!). You'll notice that in your delegate methods, you will receive events whenever you press return/done/etc.
A simple way to make the keyboard dismiss every time someone hits the return key is to implement the - (BOOL)textViewShouldEndEditing:(UITextView *)textView method.
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
[textView endEditing:YES];
return YES;
}
Edit: In your post you edited to say that you've declared both of the delegate methods... there are more than two so could you clarify what methods you've implemented? http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html#//apple_ref/occ/intf/UITextViewDelegate
Try this
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.window addGestureRecognizer:tapGesture];
//[self.view addGestureRecognizer:tapGesture];
[tapGesture release];
}
-(void)dismissKeyboard
{
[txt resignFirstResponder];
[textView resignFirstResponder];
}
Try this
That's the delegate method of textfield:-
(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[txtview resignFirstResponder];
return NO;
}
I can provide code if needed, however my problem looks fundamendal. I have a UITextField in a view that can copy and paste in it. After the action I cannot do it again. It works only once.
What might be the reason behind it? Is it possible that the paste menu is not shown because of another view in the window?
some code:
myTextField = [[UITextField alloc] initWithFrame:CGRectMake(0,1,320,50)];
[myTextField setFont:[UIFont boldSystemFontOfSize:40]];
[myTextField setTextColor:[UIColor whiteColor]];
[myTextField setText:#""];
[myTextField setBorderStyle:UITextBorderStyleNone];
[myTextField setEnabled:YES];
[myTextField setKeyboardType:UIKeyboardTypePhonePad];
[myTextField setDelegate:self];
myTextField.inputView = hiddenView;
and
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if(action == #selector(paste:))
return YES;
return NO;
}
Do I need to add something in the viewWillAppear method related with the UITextField? As I said the first time works fine.
UPDATE: After the first paste the copy/paste/select mechanism stopped working on my application in ALL views...
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (sel_isEqual(action, #selector(paste:)))
{
return YES;
}
return [super canPerformAction:action withSender:sender];
}
I have two text fields in my view. I did it using IB. My problems are
After entering the text in textField1 I am entering text in textField2. When I click in textField1 again the previous text is disappeared in the textField1.
After entering the text in both the textFields, I need the keyboard to disappear. But, even I touched the return key in the keyboard layout or I touched the screen outside the text field the keyboard is not disappearing.
How can I make this.
Thank you.
- (void)viewDidLoad
{
self.title = #"Edit";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStyleDone target:self action:#selector(save)];
//[nameField becomeFirstResponder];
nameField.clearsOnBeginEditing = NO; //First text field
[nameField resignFirstResponder];
//[descriptionField becomeFirstResponder];
descriptionField.clearsOnBeginEditing = NO; //second text dield
[descriptionField resignFirstResponder];
[super viewDidLoad];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == nameField) {
[nameField resignFirstResponder];
}
else if(textField == descriptionField){
[descriptionField resignFirstResponder];
}
return YES;
}
I did in the above way but the keyboard is still not disappearing. And after entering the text in textField1 when I press return key the cursor is not going to textField2. How can I make it work ?
Thank You.
For problem 1, check the clearsOnBeginEditing property of the text field.
For problem 2, you can send the resignFirstResponder message to the text field.
See Managing Keyboard section in the UITextField documentation.
I got the result. First thing in my mistake is I did not add UITextFieldDelegate in interface. And the remaining I did the changes in code.
- (void)viewDidLoad {
self.title = #"Edit";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStyleDone target:self action:#selector(save)];
[nameField becomeFirstResponder];
nameField.delegate = self;
descriptionField.delegate = self;
[super viewDidLoad];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(#"Return Key Pressed");
if (textField == nameField) {
[nameField resignFirstResponder];
[descriptionField becomeFirstResponder];
}
else if(textField == descriptionField){
[descriptionField resignFirstResponder];
}
return YES;
}
After entering the text in textField1 if I touch return the cursor goes to textFeild2. It is working good. But when I touch the screen out of text field the keyboard is not disappearing.
Thank you.