TextField焦点顺序

本文介绍了一个iOS应用程序如何处理键盘显示和隐藏的通知。通过监听UITextFieldTextDidBeginEditingNotification等通知来跟踪文本框编辑状态,并利用UIKeyboardWillShowNotification等通知来响应键盘的显示与隐藏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**

 第一次点击

 UITextFieldTextDidBeginEditingNotification

 UIKeyboardWillChangeFrameNotification

 UIKeyboardWillShowNotification

 

 第二次点击

 UITextFieldTextDidEndEditingNotification

 UITextFieldTextDidBeginEditingNotification

 

 消失

 UIKeyboardWillChangeFrameNotification

 UIKeyboardWillHideNotification

 UITextFieldTextDidEndEditingNotification

 */

- (void)textFieldTextDidBeginEditing:(NSNotification *)notification {

    [self.stack addObject:notification];


    if ([self.stackcount] ==2) {

#warning 键盘未消失,另一文本框获得焦点

        [self.stack removeAllObjects];

    }


    // NSConcreteNotification 0x109642c60 {name = UITextFieldTextDidBeginEditingNotification; object = <CustomTextField: 0x109539f50; baseClass = UITextField; frame = (29 142; 262 40); text = ''; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; tag = 100; gestureRecognizers = <NSArray: 0x10956f260>; layer = <CALayer: 0x10953a370>>}

}


- (void)textFieldTextDidEndEditing:(NSNotification *)notification {

    [self.stack addObject:notification];


    if ([self.stackcount] ==3) {

#warning 键盘消失

        [self.stackremoveAllObjects];

    }


    // NSConcreteNotification 0x10a879190 {name = UITextFieldTextDidEndEditingNotification; object = <CustomTextField: 0x10a823660; baseClass = UITextField; frame = (29 142; 262 40); text = ''; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; tag = 100; gestureRecognizers = <NSArray: 0x10a858890>; layer = <CALayer: 0x10a823a80>>}

}


- (void)keyboardWillShow:(NSNotification *)notification {

    [self.stack addObject:notification];


#warning 键盘抬起

    [self.stackremoveAllObjects];


    /*

     NSConcreteNotification 0x109668120 {name = UIKeyboardWillShowNotification; userInfo = {

     UIKeyboardAnimationCurveUserInfoKey = 7;

     UIKeyboardAnimationDurationUserInfoKey = "0.25";

     UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";

     UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 676}";

     UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 460}";

     UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 568}, {320, 216}}";

     UIKeyboardFrameChangedByUserInteraction = 0;

     UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 352}, {320, 216}}";

     }}

     */

}


- (void)keyboardWillHide:(NSNotification *)notification {

    [self.stack addObject:notification];


    /*

     NSConcreteNotification 0x10a863540 {name = UIKeyboardWillHideNotification; userInfo = {

     UIKeyboardAnimationCurveUserInfoKey = 7;

     UIKeyboardAnimationDurationUserInfoKey = "0.25";

     UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";

     UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 460}";

     UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 676}";

     UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 352}, {320, 216}}";

     UIKeyboardFrameChangedByUserInteraction = 0;

     UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 568}, {320, 216}}";

     }}

     */

}


- (void)keyboardWillChangeFrame:(NSNotification *)notification {

    [self.stack addObject:notification];


    /*

     NSConcreteNotification 0x109414690 {name = UIKeyboardWillChangeFrameNotification; userInfo = {

     UIKeyboardAnimationCurveUserInfoKey = 7;

     UIKeyboardAnimationDurationUserInfoKey = "0.25";

     UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";

     UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 676}";

     UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 460}";

     UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 568}, {320, 216}}";

     UIKeyboardFrameChangedByUserInteraction = 0;

     UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 352}, {320, 216}}";

     }}

     */

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值