show subview when UITextField touched - iphone

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];

Related

How To Resign The UITextView With Done Button

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];
}

iOS 5: Hide keyboard in UITableViewController with static cells and textfields

I'm developing an app that has an UITableViewController with static cells. Those static cells are custom ones and have UITextFields within them. If the user touches one of those textfields, the keyboard pops up. So, after that, what I need to do is to be able to dismiss the keyboard by touching anything on the background. By anything I mean the table view background, its cells and the content of those cells (like the text fields for example).
I used to do this by placing a View in the front of all my other subviews and capture the Touch Inside Up event to dismiss the keyboard. Unfortunately, I can't do this this time because I'm using a UITableViewController and I can't switch it back to an UIViewController because I've already done a lot of work with those static cells.
-(void)touchesBegan: (NSSet *)touches withEvent:(UIEvent *)event{
[yourTextField resignFirstResponder];
}
Try that.
EDIT
Hi,
I've got your answer :
-(BOOL)isFirstResponder
When you enter something in a text field, it becomes first responder and the keyboard appears. You know that the keyboard is active if [textField isFirstReponder] returns YES.
You may also see that link.may be it will help you.
how to hide the keyboard when empty area is touched on iphone
Well, I found the answer myself. What I need to do was just place programatically an UIView in the foreground and do as I said in the original post. I couldn't do this before because I was trying to achieve that from the Interface Builder.

How to hide the iPhone keyboard? resignFirstResponder does not work

I have the following code...
- (void)textFieldDidBeginEditing:(UITextField *)textField {
//some code here...
NSInteger theTag = textField.tag; //I set the tag to 5 in IB
if (theTag == 5) {
//self.showDatePicker;
[textField resignFirstResponder];
}
}
The problem is, the keyboard never disappears. Another thing to note is that I have some other methods that move the view up and down based on the position of the textfield selected. Maybe that's messing up my Responder, but I just don't understand why the keyboard won't go away.
Also, I might just be doing this all wrong. I want this textField, when pressed, to hide the keyboard and show a date picker. Should this be in a different method?
If you're targeting iOS 3.2+, I'd suggest looking into UITextField's inputView property. You can assign a custom view (i.e. a data picker) to be displayed when the text field becomes the first responder instead of the keyboard.

Tapping a UIScrollView to hide the keyboard?

(I posted this before anonymously, but then couldn't use the same computer again, so I'm posting it from my account now. Sorry to the guy who answered before.)
I'm working on an iPhone app which involves typing stuff into a UITextView, which adds content to a UITableView. The problem is, I need to be able to close the keyboard when the user's done with it, and the only area that is really visible other than the keyboard and UITextView at this point is the UITableView. I'm having trouble implementing a touch event on the UITableView (as in, touching the UITableView anywhere, not just didSelectRowAtIndexPath:). Here's the code I'm using in the view controller, but for some reason, it's not being executed at all:
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[textView resignFirstResponder];
}
Any suggestions?
"Here's the code I'm using in the view controller"
That's your problem - you have to create a subclass of UITableView and put the function in there before you will get those touch events.
Put an invisible button over the UITableView and trap the taps.
Yeah, I just said put a breakpoint in to make sure resignFirstResponder is actually being called and that it is being called on the correct text field.

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.