Keyboard Iphone - iphone

Is possible to know when the user touch the keyboard iphone? When the user touch some button from keyboard... :/

The easiest way is to use a TextField. Even is your UI Does not call for one, you can set it's frame to zero so it doesnt show up onscreen. Then you can get access to the keys pressed by using the text field's delegate callback methods.
- (void)viewDidLoad {
[super viewDidLoad];
//CGRectZero because we don't want the textfield to be shown onscreen
UITextField *f = [[UITextField alloc] initWithFrame:CGRectZero];
//We set the delegate so we can grab keypressed
f.delegate = self;
[self.view addSubview:f];
[f becomeFirstResponder]; //Show the keyboard
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
if (string.length >0) {
NSLog(#"%# Pressed",string);
}
else {
NSLog(#"Backspcae pressed");
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(#"return pressed");
return YES;
}
Note: to avoid a compiler warning, make sure in your .h file the class explicitly says it implements the UITextFieldDelegate protocal. ie:
#interface MyViewController : UIViewController <UITextFieldDelegate>

Related

Subclass of UITextField

I want to subclass UITextField and have a method that sets the maximum number of characters. allowed. In the init method of this subclass I try setting the inputDelegate to self but the callbacks aren't triggered. Also this is an iOS 5 thing only. My question is what protocol or notification would allow me to fire an event when a key is tapped so that I don't have to implement the UITextField delegate in my view controller to achieve this limit.
Thanks.
You could do it like this, create a delegate class for you textfield
.m file
#implementation LimitedTextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField.text.length >= 5 && ![string isEqualToString:#""])
return NO;
return YES;
}
#end
.h file
#interface LimitedTextFieldDelegate : UITextField<UITextFieldDelegate>
#end
in the UITextField subclass do the following
- (id)init
{
self = [super init];
if (self) {
//limitedDelegate is a property of your textfield subclass
self.limitedDelegate = [[LimitedTextFieldDelegate alloc] init];
self.delegate = self.limitedDelegate;
}
return self;
}

Hide keyboard/ resignFirstResponder forcibly

i'm working on an app which has a tableView with a textField in the right side of its each cell(there are more than 20 cells).
i've created custom cell's for each row except for the last one.
In the last row there is only a button.
Now i want to call resignFirstResponder on the button's click.
What should i do Please help?
You will have to keep track of which textfield in which cell has the first responder and resign it like this.
[myCellTextField resignFirstResponder];
You probably want to keep track of the text field with the keyboard. Implement the <UITextFieldDelegate> protocol in your controller, and set the controller as each of the text fields' delegates. Write the textFieldDidBeginEditing: method like so, setting an instance variable called currentTextField:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
currentTextField = [textField retain];
}
Then, in your action for the button run [currentTextField resignFirstResponder].
Aopsfan's answer is probably the best solution so far. However, to add to it (as I cannot post comments), do remember to deallocate the object:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (currentTextField != nil) {
[currentTextField release];
}
currentTextField = [textField retain];
}
Better still use #property's and #synthesize so the runtime can do the memory management for you.
[ViewController].h
#property (nonatomic, retain) UITextField* currentTextField;
[ViewController].m
#synthesize currentTextField = _currentTextField;
- (void)viewDidLoad|Appear {
self.currentTextField = nil;
}
- (void) dealloc {
[_currentTextField release], _currentTextField = nil;
...
[super dealloc];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.currentTextField = textField;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.currentTextField) {
[self.currentTextField resignFirstResponder];
}
}
I think this link will help you:
Objective C: ResignFirstResponder with a button
Provide some code so that, it will be easier to help u.
Hope this helps you. :)

Non-editable UITextField with copy menu

Is there a way to make a UITextField non-editable by the user and provide only a copy menu without subclassing? I.e when the text field is touched it automatically selects all the text and only shows the copy menu.
If possible to do this without subclassing what are the interface builder options that need to be selected?
Thanks in advance.
simply do:
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return NO;
}
and also:
yourTextField.inputView = [[UIView alloc] init];
You can disable editing by setting the enabled property of UITextField to NO.
textField.enabled = NO;
Note that doing this will also disable the copy/paste options.
What you are asking for has been discussed before in this solution: Enable copy and paste on UITextField without making it editable
Without subclassing, but also without auto-selection, i.e the user can select, keyboard shows up, but the user cannot input data:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return NO;
}
Items required to make a UITextField (in this case, outputTextField)
able to accept touch and allow Select, Select All, Copy, Paste, but
NOT edit the area and also DISABLE popup keyboard:
Put "UITextFieldDelegate" in angle brackets after ViewController declaration in #interface
In the -(void)viewDidLoad{} method add the following two lines:
self.outputTextField.delegate = self;
self.outputTextField.inputView = [[UIView alloc] init];
Add delegate method (see actual method below):
textField: shouldChangeCharactersInRange:replacementString: { }
// ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#end
// ViewController.m
#import "ViewController.h"
#interface ViewController () <UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UITextField *inputTextField;
#property (weak, nonatomic) IBOutlet UITextField *outputTextField;
- (IBAction)copyButton:(UIButton *)sender;
#end
#implementation ViewController
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
NSLog(#"inside shouldChangeCharactersInRange");
if (textField == _outputTextField)
{
[_outputTextField resignFirstResponder];
}
return NO;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.outputTextField.delegate = self; // delegate methods won't be called without this
self.outputTextField.inputView = [[UIView alloc] init]; // UIView replaces keyboard
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)copyButton:(UIButton *)sender {
self.outputTextField.text = self.inputTextField.text;
[_inputTextField resignFirstResponder];
[_outputTextField resignFirstResponder];
}
#end
This worked for me to have UITextField non Editable and only allow copy. Just change your UITextField Class to CopyAbleTF from XIB and you are set.
#implementation CopyAbleTF
- (void)awakeFromNib{
self.inputView= [[UIView alloc] init];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == #selector(copy:))
{
return true;
}
return false;
}
- (void)copy:(id)sender {
UIPasteboard *board = [UIPasteboard generalPasteboard];
[board setString:self.text];
self.highlighted = NO;
[self resignFirstResponder];
}
#end

enabling UIBarButtonItem when textfield has input

I am trying to disable my send button (UIBarButtonItem within a toolbar) whenever there is no input in the "userInput" UITextField, and enable it when there is. Here is the code I've written - I can't quite figure out why it isnt working.
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (userInput.text.length >0) {
[sendButton setEnabled:YES];
}else{
[sendButton setEnabled:NO];
}
}
Im also getting a warning that says "control reaches end of non-void function."
Im very new to xcode and programming so I'm not sure what that is telling me.
Thanks for any help!!
The text field object sends the message shouldChangeCharactersInRange to its delegate asking whether the change (adding the new character or removing a character) should be permitted or not. Since you don't want to refuse any changes made to the text field itself, you must return YES from this method. The warning
control reaches end of non-void function
means you have a non-void function so you are supposed to return something. Since nothing was being returned, this message popped up.
One important thing to remember is that this delegate method is called before the change is made, which makes sense because the delegate is being asked for permission to allow or disallow the change. If the delegate refuses, the text field's value will remain the same regardless of what the user types.
So calling userInput.text is useless because it will give the old value back as the change hasn't been made yet. However, there is enough information in the parameters of this method to construct the new to-be value of the text field.
NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string]);
The complete method would look like,
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
BOOL isFieldEmpty = [newText isEqualToString:#""];
sendButton.enabled = !isFieldEmpty;
return YES;
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (userInput.text.length >0) {
[sendButton setEnabled:YES];
}else{
[sendButton setEnabled:NO];
}
return YES;
}
ok you need in set enable NO in vieWillAppear
-(void)viewWillAppear:(BOOL)animated
{
[self.sendButton setEnabled:NO];
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (userInput.text.length >0) {
[self.sendButton setEnabled:YES];
}else{
[self.sendButton setEnabled:NO];
}
return YES;
}
make UIBarButton as property and IBOUtlet and make proper connection
.h
UIBarButton *sendButton;
#property(nonatomic,retain) IBOutlet UIBarButton *sendButton;
and in .m
#synthesize sendButton;
and
-(void)dealloc
{
[self.sendButton release];
[super dealloc];
}
and also remember to make connection for textField from IB.
In your controller add (weak) outlets for both the UITextView and UIBarButtonItem and hook them up to the items in view.
Add another IBAction to controller for example:
- (void)textChanged:(id)sender
{
if ( [self.someTextField.text length] > 0 )
[self.someBarButtonItem setEnabled:YES];
else [self.someBarButtonItem setEnabled:NO];
}
Hook up UITextView's Edit Changed event to the textChanged: you've just created.
The Edit Changed even fires up when characters are typed into UITextField, you check the length of the text and enable/disable UIBarButtonItem when the text field has some text.
You can also in viewDidLoad: check the initial text length and enable/disable the button depending on the text.
Hope that helps.
It works for me in iOS 6.1.

UITextField keyboard Issue

I have such a problem: I have a UITextField in my UITableViewCell. When I tap on that text field -> keyboard appears, but when I press Enter button keyboard don't disappear. I need such a behavior for my text field and keyboard:
When I pressed Enter, Esc - keyboard must disappear.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Where textField is UITextField in UITableViewCell
Implement textFieldShouldReturn: method in textField's delegate and call [textField resignFirstResponder] there - that will hide keyboard when return key is pressed.
I'm not sure if that will work for 'Esc' as well, but there's no such key on real device anyway so it must not be a problem
Try this
[txtField setReturnKeyType:UIReturnKeyDone];
txtField.enablesReturnKeyAutomatically=YES;
This may be an old post but I found it searching for the answer so chances are someone else might so don't shoot me for posting.
Just wanted to add
don't forget to make the delegate connection in IB for the UITextField
#interface Untitled2ViewController : UIViewController <UITextFieldDelegate>
{
IBOutlet UITextField *text;
}
#property (nonatomic, retain) IBOutlet UITextField *text;
#end
//m file
#import "Untitled2ViewController.h"
#implementation Untitled2ViewController
#synthesize text;
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
In xib file I set: Return Key: Done, Auto-anabling return key. I also tried without it, and still no reaction. Keyboard don't hides.
Write this code to create UITextLabel
UITextField *username = [[UITextField alloc]initWithFrame:CGRectMake(10.0f, 10.0f, 110.0f, 30.0f)]
[username setReturnKeyType:UIReturnKeyNext];
[username setDelegate:self];
[self.view addSubview:username];
Now to resign write this code.
-(void)resignKeyboard
{
if([username isEditing])
{
[username resignFirstResponder];
}
}
I hope it works for you.
try this
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
or this is for anywhere in the view
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}