代码之家  ›  专栏  ›  技术社区  ›  Shawn

-(void)切换到另一个uitextfield时未调用键盘

  •  3
  • Shawn  · 技术社区  · 15 年前

    我有个奇怪的问题,我不明白。我有一个带有几个uitextfield对象的uiscrollView。当我切换到视图时,我将第一个UIExtField设置为FirstResponder,并且由于该视图注册的uikeyboarddidshowNotification,keyboardwasshown方法被调用。奇怪的是,当我触摸下一个uitextfield时,没有调用keyboardwasshown方法。我不理解这一点,因为苹果公司的文档中说,“如果你的界面有多个文本字段,用户可以点击它们之间的内容来编辑每个字段中的值。然而,当这种情况发生时,键盘不会消失,但系统仍然会在每次编辑开始时在新的文本字段中生成uikeyboarddidshownotification通知。“我的代码也直接从Apple的文档中复制,它工作正常,但它只会被调用。”第一次。我错过了什么?

    - (void)registerForKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardDidHideNotification object:nil];
    }
    
    - (void)keyboardWasShown:(NSNotification *)aNotification {
    //if (keyboardShown) return;
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size;
    CGRect bkgndRect = activeField.superview.frame; 
    bkgndRect.size.height += kbSize.height; 
    [activeField.superview setFrame:bkgndRect]; 
    [scrollView setContentOffset:CGPointMake(0.0, activeField.frame.origin.y) animated:YES]; 
    keyboardShown = YES;
    
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)];
    self.navigationItem.rightBarButtonItem = doneButton;
    [doneButton release];
    }
    
    1 回复  |  直到 12 年前
        1
  •  1
  •   Jason Coco superfell    15 年前

    每次更改第一个响应者时,不会发布此通知。为此,您必须为uitextfield或uitextview实现委托协议(取决于您使用的是什么),并从那里处理这些更改。只有当键盘以前被隐藏时,才会收到显示键盘的通知。