从单独的班级中辞去第一响应者

发布于 2024-11-16 04:33:05 字数 122 浏览 3 评论 0原文

我有一个类,它制作一个键盘工具栏,上面有“下一步”、“上一个”和“完成”按钮。有没有办法让这个类随时知道(或找出)屏幕上有哪些对象?

例如,它能否看到当前视图是什么以及其上的文本字段是什么,然后能够放弃第一响应者?

I have a class that makes a keyboard toolbar which has "Next", "Previous", and "Done" buttons on it. Is there a way for this class to know (or find out) what objects are on the screen at any time?

E.g., can it see what the current view is and what the text fields on it are, and then be able to resign the first responder?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静谧幽蓝 2024-11-23 04:33:05

如果您特别想辞职第一响应者,而不需要知道哪个视图是第一响应者,您可以将 resignFirstResponder 发送到“nil”,如下所示:

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];

这是记录的行为,尽管我现在无法在文档中找到。

If you specifically want to resign first responder without the need to known which view is the first responder you can send resignFirstResponder to "nil" like this:

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];

This is documented behaviour although I cannot find in the docs right now.

维持三分热 2024-11-23 04:33:05

有没有办法让这个类知道
(或找出)上面有什么物体
当时的画面?

找到 momma 视图,您可以像这样迭代屏幕上的所有对象(因为它们也是 UIView)。请注意,您可能需要添加递归:

for (UIView *view in mommaView.subviews) {
    do something to the view
}

Is there a way for this class to know
(or find out) what objects are on the
screen at the time?

Find the momma view and you can iterate through all the objects on the screen (because they will be UIViews too) like this. Note that you may need to add recursion:

for (UIView *view in mommaView.subviews) {
    do something to the view
}
許願樹丅啲祈禱 2024-11-23 04:33:05

您可以从 Window 类开始,然后从那里向下,询问 [view respondsTo:@selector(isFirstResponder) &&每个上的 [view isFirstResponder]。我使用的一些调试代码可能会作为模板以及在调试时派上用场:

+ (void) dumpWindowFrom:(NSString *) fromText {
    [self dumpViews:[[UIApplication sharedApplication] keyWindow] from:fromText];
}

void dumpViewsRecursive(UIView* view, NSString *text, NSString *indent) 
{
    Class cl = [view class];
    NSString *classDescription = [cl description];
    //  while ([cl superclass])   //restore to print superclass list
    //  {
    //      cl = [cl superclass];
    //      classDescription = [classDescription stringByAppendingFormat:@":%@", [cl description]];
    //  }

    if ([text compare:@""] == NSOrderedSame)
        NSLog(@"%d: %@ %@ %@", (int)view, classDescription, NSStringFromCGRect(view.frame), view.hidden ? @"Inv" : @"Vis");
    else
        NSLog(@"%d: %@ %@ %@ %@", (int)view, text, classDescription, NSStringFromCGRect(view.frame), view.hidden ? @"Inv" : @"Vis");

    for (NSUInteger i = 0; i < [view.subviews count]; i++)
    {
        UIView *subView = [view.subviews objectAtIndex:i];
        NSString *newIndent = [[NSString alloc] initWithFormat:@"  %@", indent];
        NSString *msg = [[NSString alloc] initWithFormat:@"%@%d:", newIndent, i];
        dumpViewsRecursive (subView, msg, newIndent);
        [msg release];
        [newIndent release];
    }
}

+ (void) dumpViews: (UIView *) view {
    dumpViewsRecursive  (( (!view) ? [[UIApplication sharedApplication] keyWindow] : view), @"" ,@"");
}

+ (void) dumpViews: (UIView *) view from:(NSString *) fromText{
    dumpViewsRecursive ((!view) ? [[UIApplication sharedApplication] keyWindow] : view, fromText, @"");
}

You can start at the Window class and go down from there, asking [view respondsTo:@selector(isFirstResponder) && [view isFirstResponder] on each. Some debugging code that I use might come in handy as a template and also while debugging:

+ (void) dumpWindowFrom:(NSString *) fromText {
    [self dumpViews:[[UIApplication sharedApplication] keyWindow] from:fromText];
}

void dumpViewsRecursive(UIView* view, NSString *text, NSString *indent) 
{
    Class cl = [view class];
    NSString *classDescription = [cl description];
    //  while ([cl superclass])   //restore to print superclass list
    //  {
    //      cl = [cl superclass];
    //      classDescription = [classDescription stringByAppendingFormat:@":%@", [cl description]];
    //  }

    if ([text compare:@""] == NSOrderedSame)
        NSLog(@"%d: %@ %@ %@", (int)view, classDescription, NSStringFromCGRect(view.frame), view.hidden ? @"Inv" : @"Vis");
    else
        NSLog(@"%d: %@ %@ %@ %@", (int)view, text, classDescription, NSStringFromCGRect(view.frame), view.hidden ? @"Inv" : @"Vis");

    for (NSUInteger i = 0; i < [view.subviews count]; i++)
    {
        UIView *subView = [view.subviews objectAtIndex:i];
        NSString *newIndent = [[NSString alloc] initWithFormat:@"  %@", indent];
        NSString *msg = [[NSString alloc] initWithFormat:@"%@%d:", newIndent, i];
        dumpViewsRecursive (subView, msg, newIndent);
        [msg release];
        [newIndent release];
    }
}

+ (void) dumpViews: (UIView *) view {
    dumpViewsRecursive  (( (!view) ? [[UIApplication sharedApplication] keyWindow] : view), @"" ,@"");
}

+ (void) dumpViews: (UIView *) view from:(NSString *) fromText{
    dumpViewsRecursive ((!view) ? [[UIApplication sharedApplication] keyWindow] : view, fromText, @"");
}
走野 2024-11-23 04:33:05

是的,只要文本字段变为活动状态,就会调用下面提供的方法。我认为您正在寻找

- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return 1;
}

- (void) textFieldDidBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}

- (void) textFieldDidEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}

,如果您正在视图中寻找特定的文本字段,您应该为它们分配标签:

textField.tag =1 // for textField 1
textField.tag =2 // for textField 2

// You may check for these tags and then resign specific ones. 

yes, the methods provided below will be called whenever a textField becomes Active. I think you are looking for

- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return 1;
}

or

- (void) textFieldDidBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}

- (void) textFieldDidEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}

and if you are looking for a specific textField in your view, you should assign them tags:

textField.tag =1 // for textField 1
textField.tag =2 // for textField 2

// You may check for these tags and then resign specific ones. 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文