【发布时间】:2010-05-14 12:53:20
【问题描述】:
我的应用程序视图上有一些文本字段和文本视图。当我点击其中任何一个时,键盘就会出现。我有一个方法,当这些对象中的任何一个被点击时都会被调用。但是,我希望该方法仅在点击某个文本字段或某个文本视图时才执行其代码。因此,我希望在方法主体中有这样的内容:
{
if(currentField != mySpecialField)
{return;}
//Rest of the method code...
}
现在,我的问题是,如何获得对当前点击的字段的引用,以便我可以执行 if-check。
更新:
在我的情况下,我正在使用从 Apple 网站获得的代码,该代码利用了这些方法:
// 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;
}
我可以修改这些方法以获取对调用者 UITextField 和/或调用者 UITextView 的引用吗?
更新:
这是用于注册的方法:
// 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];
}
有什么方法可以实现我想要实现的目标吗?也许如果我修改上面的代码以传递 'object:self' 而不是 'object:nil'?
【问题讨论】:
标签: iphone objective-c