I have a few text fields and text views on my application's view. The keyboard comes up when I click in anyone of them. I have a method that gets called when any one of these objects is tapped in. However, I would like the method to execute its code only if the a certain Text Field(s) or a certain Text View(s) is tapp开发者_运维问答ed in. I would therefore like to have something like this in the method body:
{
if(currentField != mySpecialField)
{return;}
//Rest of the method code...
}
Now, my question is, how do I get a reference to the currently tapped in field so that I can perform the if-check.
Update:
In my situation, I am using the code I got from Apple's website that makes use of these methods:
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
if (keyboardShown)
return;
NSDictionary* info = [aNotification userInfo];
// Get the size of the keyboard.
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Resize the scroll view (which is the root view of the window)
CGRect viewFrame = [scroller frame];
viewFrame.size.height -= keyboardSize.height;
scroller.frame = viewFrame;
// Scroll the active text field into view.
CGRect textFieldRect = [activeField frame];
[scroller scrollRectToVisible:textFieldRect animated:YES];
keyboardShown = YES;
}
// Called when the UIKeyboardDidHideNotification is sent
- (void)keyboardWasHidden:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
// Get the size of the keyboard.
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Reset the height of the scroll view to its original value
CGRect viewFrame = [scroller frame];
viewFrame.size.height += keyboardSize.height;
scroller.frame = viewFrame;
keyboardShown = NO;
}
Can I modify these methods in such a way that I can get a reference to the caller UITextField and / or the caller UITextView?
Update:
Here's the method that's used for registration:
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasHidden:)
name:UIKeyboardDidHideNotification object:nil];
}
Is there any way I can achieve what I'm trying to achieve? Maybe if I modify the code above to pass 'object:self' instead of 'object:nil'?
Controls that are subclasses of UIControl
(for example UIButton
or UITextField
) automatically sends a reference to themselves along to their target.
When creating your text fields, just specify a selector that takes one argument. In this example, "myTextField" is an instance variable:
myTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 100, 40)];
[myTextField addTarget:self action:@selector(textFieldTapped:) forControlEvents:UIControlEventTouchUpInside];
And then the method we specified, textFieldTapped:
, to handle the event:
- (void)textFieldTapped:(UITextField *)sender {
// Ignore any events that doesn't come from myTextField
if (sender != myTextField) return;
// TODO: Do stuff
}
In the case of UITextField
you can also check out the UITextFieldDelegate
protocol (the "delegate" value of your text field). It sends out various events that can be used to register the same kind of things, for example:
- (void)textFieldDidBeginEditing:(UITextField *)textField
精彩评论