how to handle key events in iphone - iphone

Hi
I am working on an iphone application and want to handle keyboard events in iphone. In Mac, there is a class NSEvent which handles both keyboard and mouse events, and in ios (iphone/ipad) the counterpart of NSEvent is UIEvent which handles only touch events. I know ios API does not provide this functionality, but how can i handle key events in iphone??? Any good tutorial or sth, to get started...

You cant directly code for keyboad;s key and there is no mouse in case of device.
you can make your logics for different kind of charectersets or you can make your logics in textField delgate methods or Textview Delegates method
textView delegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
textField delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
You can also use Notification for textField and Textview.
For TextField use this
call this register method in viewDidLoad
-(void)registerForTextFieldNotifications {
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:#selector (handle_TextFieldTextChanged:)
name:UITextFieldTextDidChangeNotification
object:self.textField];
}
- (void) handle_TextFieldTextChanged:(id)notification {
if([iSinAppObj.passCodeString isEqualToString:lockTextField.text])
{
//code here
}
}
and for text view you need to change only event name like this
[notificationCenter addObserver:self
selector:#selector (handle_TextFieldTextChanged:)
name:UITextViewTextDidChangeNotification
object:self.textField];

You can use notifications to handle events as explained here:
http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html
But the functionality is very limited.

Related

How to call method on return key and also on ipad down arrow key

I have method for textfield animation which is running on after resignfirsrresponder i want that method also be called when user presses down arrow of the ipad which hides keyboard any idea how to do this. thanks.
I have added screenshots the button beside ABC text button i when we press that button normally keyboards hides i want to call animation on that button click.
For Ipad down arrow key You can use notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
-(void)keyboardWillHide
{
//call when key board hides
}
You can call the textfield animation method in any one of the following UITextFieldDelegate methods
- (void)textFieldDidEndEditing:(UITextField *)textField {
}
or
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
return YES;
}
just write your code inside
- (void)textFieldDidEndEditing:(UITextField *)textField {
}
And don't forget to bind textfield delegate and write <UITextfieldDelegate> in .h file
if you want use done button on key board take one view with done button and write this method
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField setInputAccessoryView:self.doneView];
}
and then give following action to that done button
-(IBAction)doneButtonPressed:(id)sender
{
[currentTextField resignFirstResponder];
}

What is the notification that is sent when the TextView ends editing?

What is the notification that is sent when the TextView ends editing ? I tried :
- (void)TextViewDidEndEditing:(UITextView *)textView
but it doesn't work.
This notification will be called
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
Notification is
UITextViewTextDidEndEditingNotification
Try this in view did load
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:#selector(centerViewOnKeyboardDismissal:)
name:UITextFieldTextDidEndEditingNotification object:nil];
or
change method name
- (void)TextViewDidEndEditing:(UITextView *)textView
to
- (void)textViewDidEndEditing:(UITextView *)textView
The delegate method when TextView ends editing is:
- (void)textViewDidEndEditing:(UITextView *)textView
The issue with your delegate is you started the method name with Capital letter. That's why it's not working.
Please refer the UITextViewDelegate_Protocol Reference

UITextField text change callback

I have a UITextField named textFieldInput and some button. Somehow I disable the input view so that if anyone tap in the texField no keyboard will show. I am adding text when a button is pressed programmatically. And I want to catch this changes. I want to call a function when the text of textField will change. How Can I do that?
I tried by adding following function
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)replacementStr {
// some my code
return YES;
}
But this does not work. this only calls when i tap on the textField.
I also tried by adding following in my viewDidLoad function
[textFieldInput addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
this also doesn't work.
How can I do this?
Thanks.
Register notification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(textChanged:)
name:UITextFieldTextDidChangeNotification
object:YOUR_TEXT_FIELD];
shouldChangeCharactersInRange:
this will work only when you set its delegate with the controller which implements this method. one other way is:
add these line viewDidLod
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:#selector (handle_TextFieldTextChanged:)
UITextFieldTextDidChangeNotification
object: textFieldInput];
and implement handle_TextFieldTextChanged:
- (void) handle_TextFieldTextChanged:(id)notification {
// write your logic here.
}

textFieldShouldBeginEditing + UIKeyboardWillShowNotification + OS 3.2

I have multiple textfields on a UIView.
I resign for a previous textField in textFieldShouldBeginEditing method, where following sequence of events are performed
UIKeyboardWillHideNotification is received corresponding to that field where the keyboard for the previous field is hidden.
the method textFieldShouldBeginEditing returns a YES and then
UIKeyboardWillShowNotification is received where the keyboard for the current field is displayed.
However, in OS 3.2 even though textFieldShouldBeginEditing returns a YES, UIKeyboardWillShowNotification for the current field is not received.
The logic works for OS < 3.2
Any ideas where I might be doing wrong?
Listed below a part of my code (with only two text fields in xib).
I need to perform a set of operations at keyboardWillShow and keyboardWillHide Look at the difference on running the code in OS 3.2 and OS < 3.2
Can anyone explain the difference in behaviour?
.h
#interface ExampleViewController : UIViewController
{
IBOutlet UITextField *numericTextField;
IBOutlet UITextField *alphaTextField;
UITextField *lastTextField;
int lastCursorPos;
int cursorPosition;
NSMutableArray *textFields;
}
#property (nonatomic, retain) UITextField *lastTextField;
#property (nonatomic, retain) NSMutableArray *textFields;
#end
.m
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:self.view.window];
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.textFields = [[NSMutableArray alloc] initWithCapacity:2];
[self.textFields insertObject:alphaTextField atIndex:0];
[self.textFields insertObject:numericTextField atIndex:1];
cursorPosition = 1;
[numericTextField becomeFirstResponder];
}
-(void)viewWillDisappear:(BOOL)animated
{
[self setEditing:NO animated:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
int index;
for(UITextField *aField in self.textFields){
if (textField == aField){
index = [self.textFields indexOfObject:aField];
}
}
if(index>=0 ){
lastCursorPos = cursorPosition;
self.lastTextField = [self.textFields objectAtIndex:lastCursorPos-1];
cursorPosition = index +1;
}
[self.lastTextField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (void)keyboardWillShow:(NSNotification *)notif {
NSLog(#"Inside keyboardWillShow");
}
- (void)keyboardWillHide:(NSNotification *)notif {
NSLog(#"Inside keyboardWillHide");
}
I believe that as of iOS 3.2, UIKeyboardWillHideNotification and UIKeyboardWillShowNotification are no longer fired when switching between two text fields. Basically, the notifications only fire if the keyboard is actually shown or hidden, and since switching from one text field to another doesn't hide the keyboard, the event doesn't fire.
Prior to iOS 3.2 the events used to fire whenever you changed fields. The new way is arguably more correct, but it does make what you are trying to do a bit more challenging.
You might be better off implementing the delegate for the text fields, then you can check for the shouldBeginEditing/didEndEditing events, or alternatively, you could subclass UITextField and override the becomeFirstResponder/resignFirstResponder methods so that you can hook into them and implement your logic when the fields receive and lose focus.
I think you are trying to change the keyboard types when you are on a particular text field. Instead of tracing it the way your doing simply use the two methods,
- (void)textFieldDidBeginEditing:(UITextField *)textField;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
The first method is called whenever you touch a textfield for editing.
Here you can write you keyboard changing code
EG: If textfield is of type 1
set Keyboard Type to alphanumeric.
Else if textfield is of type 2
set Keyboard Type to numeric only.
Then the second method is called whenever you press the RETURN key on the onscreen keyboard.
Here you can write the [textfield resignFirstResponder] statement for any incoming textfield control.
Hope this helps.. :) cheers!
When the keyboard appears, the method is called by notificationCenter.
If it's not working set the object to nil.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:self.view.window];

UITextfield and webservice

I have one query regarding the (UITextfield and Webservice) for example
When i start writing text in UITextfield. Make sure, the app does not query the server(Webservice) for each letter typed. It should wait till I make a pause and then send the request.
Is this possible to when using of UITextField?
Thanks in advance
You can do that using performSelector:withObject:afterDelay: and cancelPreviousPerformRequestsWithTarget: methods. Here's sample code - requestAction: gets called after user makes 2sec pause in editing text field.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
// Cancel previously registered perform request
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(requestAction) object:nil];
// schedule a call with 2 sec delay
[self performSelector:#selector(requestAction) withObject:nil afterDelay:2.0];
return YES;
}
- (void) requestAction{
NSLog(#"Request!");
}