iPhone - Problem with UITextView - iphone

This is probably an easy thing to do, but I just can't figure it out - how do I end editing athe textview? how can I get the keyboard to disappear? or do I have to click outside it to make it go away?

First, a (to be honest) fairly simple question like this makes me wonder if you've tried reading the documentation, or searching on the internet.
Searching for "Apple documentation UITextView" gives you this link to the class documentation. Similarly, here is the documentation for the UITextViewDelegate.
Searching for "UITextView simple example" gives you this useful example.
Searching for "UITextView dismiss keyboard", the first hit seems to answer your question exactly. (Although he dismisses the keyboard on a return key, which may not be what you want.) (Edit - it seems from your second comment it's exactly what you want.)
P.S. The people above are correct, if a little terse (understandably). You need to implement a UITextViewDelegate. In that delegate, if you want to hide the keyboard on a return key, implement shouldChangeTextInRange, look for a #"\n" and resign first responder if you get it. Alternatively, add a "Done editing" button to your UI, and resign first responder if the user presses it.

Very Easy:
[myTextField resignFirstResponder];
will do the trick.

One way to end editing, tapping outside the textView, is not entirely trivial. Selecting other text views or text fields or activating a navigation control will trigger...
- (void)textViewDidEndEditing:(UITextView *)textView
...on whatever object you've designated as the textView's delegate. You can trigger this yourself by calling...
- (BOOL)endEditing:(BOOL)force
...on the view that contains your text field.
Suppose I have a UITextView inside a UITableViewCell (inside a UITable). I want to enable editing to end by tapping the table. I could do this:
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(didTapTable)];
[[self tableView] addGestureRecognizer:tapRecognizer];
[tapRecognizer release];
}
- (void)didTapTable
{
[[self tableView] endEditing:YES];
}
Now whenever I tap my table, I end editing. And, as others have said, in textViewDidEndEditing I should be sure to call [textView resignFirstResponder];

[yourTextField resignFirstResponder];
will make the keyboard disappear and editing end.

Related

is it possible to disable keyboad(UITextField) without hidding?

I'd like to disable UITextField's keyboard without hidding.
is it possible?
If it's so, could you show me how to do?
thanx
Carsh
if you implement delegate method of uitextfield
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
It does not show the keyboard.
I don't believe the UIKeyboard can be disabled without hiding it.
If you'd like to disable user interaction with the keyboard, you could add an extra UIWindow on top of your existing windows.
By overlaying the UIKeyboard with a transparent UIWindow, the user would be able to see the keyboard, without interacting with it.
a haha , i have a very very ugly method. you can creat a view whichs frame = the keyboards frame. [view setBackgroundColor:[UIColor clearColor]]; [self addSubviews:view];
I did not try but this should work:
textField.inputView.userInteractionEnabled = NO;

Weird keyboard behavior in my iOS app

Ok, so here's how my keyboard behaves:
There's a button which calls a method to send a textmessage, that works fine. If the user now sends the text everything's fine.
Now if (s)he taps cancel it switches back to my view (as it's supposed to do), but the keyboard won't show up. I already tried
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
[self dismissModalViewControllerAnimated:YES];
[inputView becomeFirstResponder];
}
since it didn't work I tried:
-(void)viewDidAppear:(BOOL)animated{
[inputView becomeFirstResponder];
}
but that won't work either. So how can I get my keyboard to show up?
EDIT: inputText is an UITextView.
This might be related to sending YES in dismissModalViewControllerAnimated:. When animations are involved, statements generally don't remain synchronous. So, your call to [inputView becomeFirstResponder] must be executing before the modal dialog has been dismissed, resulting in an inconsistent state. This is the reason why the inputView does not take focus.
HTH,
Akshay

resignFirstResponder makes field lose focus but keyboard doesn't disappear

I call resignFirstResponder for my textField, it returns YES, but the keyboard does not disappear. I tried many variations and tips, but nothing helped.
May have an idea why this is possible?
I repeat, the method works, the field loses focus, but the keyboard does not disappear
I use iOS 4.3.
UPD:
if([self.securedTextView.passField isFirstResponder]){
if([self.securedTextView.passField canResignFirstResponder]){
[self.securedTextView.passField resignFirstResponder];
}
}
and [self.securedTextView.passField resignFirstResponder]; returns YES, but the keyboard is still on the screen...
you can use UITextField delegate methods explained below..and Try using this..
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
Happy Coding...

MFMailComposeViewController Keyboard Issue

How do i dismiss the keyboard without pressing the Send or Cancel button in MFMailComposeViewController?!
Thanks for any help.
Can you try this.
UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView* firstResponder = [keyWindow performSelector:#selector(firstResponder)];
[firstResponder resignFirstResponder];
hope this helps....
I experienced a similar problem: For some reason iOS does not dismiss the Keyboard of a MFMailComposeViewController when the application enters background (the dismiss happens when the application becomes active again). However iOS dismisses the keyboard if the first responder is a simple element (e.g. textview). Calling resignFirstResponder did not work for me in this particular case.
Because I switch windows on applicationBecomeActive (to show a login screen) I ended up having multiple keyboards above each other (the one on the top not working).
I found a simple workaround to dismiss the keyboard of an MFMailComposeViewController when the application resigns active:
- (void)applicationWillResignActive:(UIApplication *)application
{
// Workaround: MFMailComposeViewController does not dismiss keyboard when application enters background
UITextView *dummyTextView = [[UITextView alloc] init];
[self.window.rootViewController.presentedViewController.view addSubview:dummyTextView];
[dummyTextView becomeFirstResponder];
[dummyTextView resignFirstResponder];
[dummyTextView removeFromSuperview];
// End of workaround
}
This will implicitly resign the first responder if we have any viewController that is currently beeing presented.
While you probably can do it by finding whichever view is the first responder and calling resignFirstResponder on it (unless you're on iPad and MFMailComposeViewController uses UIModalPresentationFormSheet), Apple might reject your app for it. Quoth the documentation:
Important: The mail composition interface itself is not customizable and must not be modified by your application.
This could easily be construed to include the behavior of the keyboard.

Keyboard won't dismiss when popover closes on iOS 3.2

- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController {
[self dismissFirstResponder];
return YES;
}
-(void)dismissFirstResponder {
[nameField resignFirstResponder];
[descriptionField resignFirstResponder];
[helpField resignFirstResponder];
}
I have tried loads of different things, but the keyboard just isn't going down:
I checked to see if my outlets were hooked up correctly in Interface Builder
I put breakpoints inside the 2 methods to check they were being called at the appropriate times, and they were. Those 3 Text Fields are the only ones in the app.
What happens: The popover gets dismissed but the keyboard stays up.
I would really appreciate some help on this matter. It might be a known bug on iOS 3.2, if so any workarounds would be gratefully accepted. Thanks
Make sure the delegate for UITextView the UITextField is assigned
Then call the following method to dismiss any keyboard activity from the view.
[self.view endEditing:YES];