How to show UIDatePIcker over the TabBar like keyboard did - iphone

I Know there is few question on this site talk about this... but I don't find real answer that i can use...
I have a form that contain common UITextField and a UIButton that supposedly show UIDatePicker sliding from bottom when user click the button...
The main question is:
how to show UIDatePicker over the TabBar like iphone keyboard did
how make the UIDatePicker is behave like a keyboard, so when i click UITextField while
selecting date, it changed to keyboard and vice versa... like iphone contact did.
Thanks in advance

Set input View of your textfield to be a UIDatePicker object:
UITextField has following properties.
#property (readwrite, retain) UIView *inputView;
Use following code:
yourTextfield.inputView = yourDatePicker;
so instead of keyboard which is default for UITextfield, your picker will appear when textfield is tapped....

five years ago question.
1.create a subclass of the UIResponder class
2.redeclare inputview as read-write
#property (nonatomic, strong, readwrite) UIView *inputView;
3.set canBecomeFirstResponder to YES
- (BOOL)canBecomeFirstResponder {
return YES;
}
4.set inputview such as datepicker
self.inputView = self.datePicker;
5.set UIResponder to firstResponder when you want
[self becomeFirstResponder];
5.you can see datepicker show like keyboard

Related

How to add text area in iOS app

I am newbie for iPhone application. I want to add TextArea. I know how to add textField, however can someone help me how can I have Textare in my iPhone application?
I thought, I can increase the height of textfield and make it multi-line, however that option is not there.
You cannot increase the height of UITextField. for this you need to use UITextView.
In your Interface builder where you got UITextField, there is also an option of UITextView.
you can use UITextView same as UITextField in iPhone. UITextView is used for multiple lines
UITextView *txt = [[UITextView alloc]init];
//set frame
//add on view
[self.view addSubView:txt];
[txt release];
I hope this solves your problem Sample Code for TextView
UITextView *txt = [[UITextView alloc]initwithframe:CGRectMake(x, y, width,height)];
[self.view addSubView:txt];
[txt release];
You could simply do this by clicking on Mainstoryboard.storyboard or viewController.xib and dragging in a UITextView This will allow multi-line text. If you need to access the Textview, type:
.h file
{
IBOutlet UIITextView *textView;
}
#property (nonatomic, strong) IBOutlet UITextView* textView;
.m file
#synthesize textView;
EDIT
to get the users data, hook up the outlet in storyboards,
then use:
-(void)getUserText{
NSLog#"%#", self.textView);
}
This should print out whatever the user entered into the textView.

How can I dismiss the keyboard programmatically?

I need a way of determining the UITextField that is currently selected in a view. Is this possible without passing a reference or tag?
To be more specific I need to be able to tell which UITextField is selected so that I can hide the keyboard. The reason I need this is because I want to create a UIToolbar to add to all the UITextField's as an input accessory. On this UIToolbar I will add a 'Done' button, when pressed this should hide the keyboard for the currently selected UITextField.
I assume you mean you want to know which UITextField is the first responder (which is the text field that gets input from the keyboard).
There is no public API for this (though there is a private API). You can track which text field is the first responder manually using the textFieldDidBeginEditing: method of each text field's delegate, or you can use a little trickery to find the first responder at any time.
Here's the trick. The UIApplication object knows which object is the first responder, and can send a message to it. So you write a category like this on UIResponder:
UIResponder+firstResponderHack.h
#import <UIKit/UIKit.h>
#interface UIResponder (firstResponderHack)
+ (UIResponder *)firstResponderByHack;
#end
UIResponder+firstResponderHack.m
#import "UIResponder+firstResponderHack.h"
#interface FirstResponderFinder : NSObject
#property (strong, nonatomic) UIResponder *firstResponder;
#end
#implementation FirstResponderFinder
#synthesize firstResponder = _firstResponder;
#end
#implementation UIResponder (firstResponderHack)
- (void)putFirstResponderIntoFinder:(FirstResponderFinder *)finder {
if (self.isFirstResponder)
finder.firstResponder = self;
}
+ (UIResponder *)firstResponderByHack {
FirstResponderFinder *finder = [FirstResponderFinder new];
// Sending an action to nil sends it to the first responder.
[[UIApplication sharedApplication] sendAction:#selector(putFirstResponderIntoFinder:) to:nil from:finder forEvent:nil];
return finder.firstResponder;
}
#end
Then you can find the first responder, and check whether it's a UITextField, like this:
UIResponder *firstResponder = [UIResponder firstResponderByHack];
if (firstResponder && [firstResponder isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)firstResponder;
// do something with textField
}
There is an easy way to dismiss the keyboard without having to track the currently active control, or iterating through all the available controls, or using a UITextFieldDelegate.
[self.view endEditing:YES]
From the docs:
endEditing:
Causes the view (or one of its embedded text fields) to
resign the first responder status.
- (BOOL)endEditing:(BOOL)force
Parameters
force
Specify YES to force the first responder to resign, regardless of whether it wants to do
so.
Return Value
YES if the view resigned the first responder status or NO if it did not.
Discussion
This method looks at the current view and its subview
hierarchy for the text field that is currently the first responder. If
it finds one, it asks that text field to resign as first responder. If
the force parameter is set to YES, the text field is never even asked;
it is forced to resign.
There is a delegate method:
- (void)textFieldDidBeginEditing:(UITextField *)textField
Apple Docs:
This method notifies the delegate that the specified text field just
became the first responder. You can use this method to update your
delegate’s state information. For example, you might use this method
to show overlay views that should be visible while editing.
There is also a property:
#property(nonatomic, readonly, getter=isEditing) BOOL editing
Apple Docs:
A Boolean value indicating whether the text field is currently in edit
mode. (read-only)
Just make an ivar for the UITextView in your header file:
UITextField *editingField;
#property (nonatomic, copy) UITextField *editingField;
Then,
- (void)textFieldDidBeginEditing:(UITextField *)textField;
{
editingField = textField;
// Whatever else you want to do
}
I'm thinking that you need to diff the textFields without reference.
So, the recommended why is using ObjectiveC runtime.
It's pretty straight forward.
Firstly:
#import <objc/runtime.h>
Then, define a char for its address:
static char UITextFieldViewIdentifier;
Then set the identifier with something like this:
objc_setValue(textField, &UITextFieldViewIdentifier, #"Identifier") //typing on a phone, not so sure about the expression
In the delegate method:
NSString *identifier = objc_getObject(textField, &UITextFieldViewIdentifier)
Just call this line where you want to dismiss the keyboard:
[self.view endEditing:YES];

How to show UIKeyboard upon tapping a button?

I want to show the UIKeyboard upon tapping a UIButton and display the text (typed by the user) on a UILabel. Is this possible?
You can create a hidden UITextField, and set it as firstResponder. As you inputing any chars, copy those chars from hidden UITextField to UILabel.
Why not just replace the label with an appropriately-styled (visible) UITextField? Then swap it back when the user taps the button again (or a Done button somewhere).
This is sorta the opposite of what you want, but this this you can just mess with the code and have it make the keyboard visible on buttonpress. Also in this example, the change is triggered when the user touches the background. You can just set this to whatever button you want instead of the background.
Hiding the Keyboard when the User Taps the Background
#import <UIKit/UIKit.h>
#interface hideKeyboardViewController : UIViewController {
UITextField *textField;
}
#property (nonatomic, retain) IBOutlet UITextField *textField;
- (IBAction)textFieldReturn:(id)sender;
- (IBAction)backgroundTouched:(id)sender;
#end
Select the hideKeyboardViewController.m file and implement the action by calling the resignFirstResponder method of our textField object:
#import "hideKeyboardViewController.h"
#implementation hideKeyboardViewController
#synthesize textField;
-(IBAction)textFieldReturn:(id)sender
{
[sender resignFirstResponder];
}
-(IBAction)backgroundTouched:(id)sender
{
[textField resignFirstResponder];
}
.
.
#end
More on this subject here

How to assign multiple UIButtons to a UITextView

I want to assign two UIButtons to UITextView so that when one of the buttons is pressed the textview content should change from what it had when the previous button was pressed.
MyViewController.h:
#interface MyViewController : UIViewController
{
NSString* savedText;
}
#property(nonatomic, retain) IBOutlet UITextView* textView;
- (IBAction)buttonOnePressed:(id)sender;
- (IBAction)buttonTwoPressed:(id)sender;
#end
Connect the view controller's textView outlet to the text view in the xib file. Connect the buttons to the corresponding actions. (See Apple's Xcode tutorial if you don't know how to do this.)
MyViewController.m:
- (void)buttonOnePressed:(id)sender
{
[savedText release];
savedText = [textView.text copy];
}
- (void)buttonTwoPressed:(id)sender
{
textView.text = savedText;
}
(These aren't the complete files, of course, just the bits to make it do what you're asking.)

add something above of iPhone / iPad keyboard?

i was wondering how can i add a view on ios Keyboard ? like this :
you will want to look into the inputAccessoryView property in the UITextView or UITextField class (depending on what you are using) for a custom view above the keyboard.
If you want to only show the accessory view by itself then subclass your UIResponder view that presents the keyboard and redeclare inputView as read/write and return your accessory view in that method.
Interface
#property (atomic, retain) UIView *inputView
Implementaion
- (UIView *)inputView { return accessoryView; }
- (void)setInputView:(UIView *)aView {
if (accessoryView != aView) {
[accessoryView release];
accessoryView = [aView retain];
}
}
More information on redeclaring and adding both inputView and inputAccessoryView for any UIResponder subclass in the official Apple documentation: https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/InputViews/InputViews.html