0

I have custom UIToolbar's that sit ontop of my keyboard when it displays and that I use to insert pre-formatted text. My problem is that I have one UITextView and two UITextField's that are potential users of my pre-formatted text.

When the buttons call an IBAction, how can I tell which element has focus and is the firstResponder?

Attempt #2 at asking my question smartly:

I need to call [textElement selectedRange]; on the UITextField or UITextView that has focus. How can I declare textElement in a way that it doesn't matter which of the view UIText* classes it is?

If I do the following...

UIView *textElement = [self my_method_of_getting_first_responder];
NSRange range = [textElement selectedRange];

I of course get the

UIView does not declare selector 'selectedRange'

Thanks!

4

1 回答 1

1

如果我理解了问题,您想单击某个按钮,然后获取指向您当前活动文本字段的链接吗?然后你必须使用 UIView 上的类别:

@implementation UIView (FindFirstResponder)
- (BOOL)findFirstResponder
{
    if (self.isFirstResponder) {
        return YES;     
    }
    for (UIView *subView in self.subviews) {
        if ([subView findFirstResponder]) {
            return YES;
        }
    }
    return NO;
}
@end

您可以从视图控制器调用 UIWindow 上的此类别方法。获取窗口使用UIWindow *window= self.view.window;

于 2013-02-03T00:00:08.530 回答