从单独的类中辞退第一响应者

| 我有一个使键盘工具栏具有\“ Next \”,\“ Previous \”和\“ Done \”按钮的类。此类是否可以随时知道(或找出)屏幕上有哪些对象? 例如,它可以看到当前视图是什么,视图上的文本字段是什么,然后能够辞职第一响应者吗?     
已邀请:
           这个班有没有办法知道   (或找出)物件上   屏幕当时? 找到Momma视图,您可以像这样遍历屏幕上的所有对象(因为它们也将是UIViews)。请注意,您可能需要添加递归:
for (UIView *view in mommaView.subviews) {
    do something to the view
}
    
        如果您特别想辞职第一响应者而无需知道哪个视图是第一响应者,则可以将resignFirstResponder发送给\“ nil \”,如下所示:
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
尽管我现在在文档中找不到,但这已记录为行为。     
        您可以从Window类开始,然后从那里下来,在每个类上询问[view responsesTo:@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, @\"\");
}
    
        是的,只要textField变为Active,就会调用下面提供的方法。我想你在找
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return 1;
}
要么
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}

- (void) textFieldDidEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}
如果要在视图中查找特定的textField,则应为其分配标签:
textField.tag =1 // for textField 1
textField.tag =2 // for textField 2

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

要回复问题请先登录注册