How To Resign The UITextView With Done Button - iphone

i was wondering if any of you knew how to get access to that done button that appears above the keyboard when editing.
I have seen it before, above the keyboard there is a transparent black area and on the right there is a blue "Done" Button.
I could do this by hand with my own animations and buttons above the keyboard in my app to resign the UITextView, but i would prefer to use Apple GUI elements that people know.
So does anybody have any information about where this "Done" button comes from?

You don't really "get access" to that button, but through the UITextViewDelegate protocol, you essentially can:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html
Implement the
- (void)textViewDidEndEditing:(UITextView *)textView
routine, and assign the UIViewController holding the textview to the textview' delegate.
Inside of that routine, you can do what you wish! You will need to call the
resignFirstResponder
method.
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIResponder_Class/Reference/Reference.html#//apple_ref/occ/instm/UIResponder/resignFirstResponder
You will have to implement the style of keyboard that implements that blue done button.
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITextInputTraits_Protocol/Reference/UITextInputTraits.html#//apple_ref/occ/intf/UITextInputTraits
FINAL ANSWER
- (void)textViewDidEndEditing:(UITextView *)textView{
[textView resignFirstResponder];
}

Related

after inputAccessoryView tapped

I have tried the solutions at UITextView doesn't show InputAccessoryView on first click
I'm having a with a problem with a textView who has a inputAccessoryView
I want to call [textView resignFirstResponder] through its inputAccessoryView,so that i can close the keyboard
and there is only a UIButton on the inputAccessoryView,and the tapping the button will invoke an IBAction method call -(IBAction)closeKeyboard:(id)sender;
now the sender in the method is the button on the inputAccessoryView,
question is ,
how can i find out this textView whose inputAccessoryView has been tapped,
or just get a pointed which is pointed to this textView,so i can call
[textView resignFirstResponder]??
You need to find the current first responder. If you have outlets to all your text views, you can ask each one in turn
if ([textView1 isFirstResponder])
[textView resignFirstResponder];
Though this may give you problems depending on your button (see here, but I haven't experienced this), if so use the editing property of the text view.

show subview when UITextField touched

I'm pretty new to iPhone development, so please excuse my ignorance. I've googled this a bit and so far come up with nothing useful. Here is what I would like to do:
I would like a subview to popup(with the rest of the screen showing in the background) when a UITextField is touched. The popup is a calculator UIView that I created in the IB. It seems it is better to have a popup show than a customized keyboard, due to Apple's developer guidelines.
Here is my question. How do I capture the touch on the UITextField and how do I show the subview?
I have tried things like below to show the subview, with no luck:
CustomCalculator *customCalc = [[CustomCalculator alloc] initWithNibName:#"CustomCalculator" bundle:nil];
UIViewController *calcController = [self.customCalc.view];
[self.view addSubview:calcController.view];
Use the delegate method:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
//Add your subview here
return NO; //this will stop the keyboard from poping up
}
This way when someone taps the textfield, your view will popup instead of the keyboard.
Now once the user is interacting with your view, you will have to manipulate the string in the textfield.text property directly as a reaction to the User tapping buttons in your view.
Implement the UITextFieldDelegate and the method for it is
-(void) textFieldDidBeginEditing:(UITextField *)textField
The above method is fired when you touch the UITextField. You may then position the UIPopoverController (I'm guessing that is what you're using to show the view in a popup) and as soon as you're done there pass the values back to the UITextField. Hence the popover's/viewcontroller presented's delegate should be your textfield object.
EDIT: After seeing the other answer below it struck me that I forgot to tell you how to stop the keyboard from showing. Just make this the first line in the method I've mentioned above:
[textField resignFirstResponder];

how to check if a view is behind the keyboard on iphone

in my application i have a bunch of textlabels and textviews. Sometimes the textview is underneath the keyboard. My question is if there's a way to check if a textview is behind the keyboard to move it up. I already know how to move views up, and i know about the keyboardWillAppear notifications, but i don't know how to check if the view is behind the keyboard. The thing is that i don't want to move the textview if it's not underneath the kayboard. How can achieve that?
Thanks in advance.
I would do the check for first responder as shown above
[text isFirstResponder];
then I'd check to see if the bounds of the text field's bounds are less than 215 (because i think that's the max height of the keyboard) and accommodate from there. so all together it looks like:
if([text isFirstResponder]){
if(text.bounds.y > 215){
text.bounds.y = CGPointMake(text.bounds.y-(text.bounds.y-215));
}
}
I think the only way to see this is to verify each UITextField and UITextView if it returns YES for
[_text isFirstResponder];
If any UITextField or UITextView is First Responder, than it means that the keyboard is on the bottom of the screen.
You can see the keyboard will appear by listening to UITextFieldDelegate and UITextViewDelegate ShouldBeginEditing events:
for UITextField it is:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;// return NO to disallow editing.
and for UITextView it is:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
Hope it helps.

iPhone UITextView Did End On Exit properties?

Why is it that there is no Did End On Exit property for a UITextView? How do you resignFirstResponder to lower the keyboard when the user clicks the 'Done' button? A UITextField usually works by just linking 'Did End On Exit' from UIBuilder but UITextView doesn't have that option.
Most apps solve this problem by adding a button somewhere in your UI. For instance in Safari, when you pop up the Keyboard on a UITextView, it brings a Toolbar with it with Previous, Next, autofill and a Done Button, and the Done button implements [myTextView resignFirstResponder];
There is textViewDidEndEditing: in the UITextViewDelegate protocol that will do what you need I believe.

How to retract the Keyboard on touching the UITextView when keyboard is already up?

I am writing an application that has a UITextView which allows editing. When a user first touches UITextView, a keyboard shows up and I want to retract that keyboard when user again touches the UITextView e.g. I have entered some data in a textview and with keyboard still showing on the screen I tap on the UITextView which should cause the keyboard to retract.
Is there any way to achive this?
(I'm aware of providing a done button and doing this but I want to achive this by tapping on UITextView itself)
As an aside, I would urge you not to use toggle state elements on the iPhone. It's to easy to double tap in real world use. That is why the Apple apps all use the either the "return" key on the keyboard or the done button.
In the interface you contemplate, the users will find themselves closing and then accidentally reopening the keyboard about 10% of the time or more. It will make your app feel cumbersome and flaky.
You should call resignFirstResponder for the UITextView. Let's say you have an IBOutlet for the text view:
#property (nonatomic, retain) IBOutlet UITextView *comment;
Then [comment resignFirstResponder]; can be called from a touchesBegan or the like.
See e.g. How to Dismiss the Keyboard when using a UITextView.
What you are looking for is a large transparent uibutton "overlapButton" which always stays on top of the uitextview.
When the textview appears, you set the button hidden so you can tap on the textview freely.
[overlapButton setHidden:YES];
When tapping the textview, the keyboard will come up and the following method inside your textview delegate will get called:
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
[overlapButton setHidden:NO];
}
Here, you need to set visible the "overlapButton" so that while the keyboard is up, you can touch the button which now overlaps the textview. On the button action, you can hide the keyboard:
-(IBAction) overlapButtonTapped{
[myTextView resignFirstResponder];
}
After resigning the first responder, the following method will get called:
-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
[overlapButton setHidden:YES];
}
After setting the hidden property accordingly for the button (like above), you have a "clear" textview again which you can tap again to show the keyboard.. etc .. etc ..
Cheers.
I agree with you TechZen. I've seen this happen! I would not advise this also! It also makes editing very hard if not impossible for edit/copy/paste gestures. On the other hand, if the man still wants this badly.. :P the code provided by me in my other post works a treat! Cheers.