首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS UITextField resignFirstResponder on viewWillDisappear

iOS UITextField resignFirstResponder on viewWillDisappear
EN

Stack Overflow用户
提问于 2017-01-25 07:53:43
回答 2查看 584关注 0票数 3

我有一个有两个简单屏幕的应用程序。第一个屏幕总是打开第二个屏幕的同一个实例。第二个屏幕有一个text field和一个button来手动隐藏keyboard

resignFirstResponder on viewWillDisappear不起作用。

案例1:

  • 单击主屏幕上的openSearchButton。
  • SearchViewController被打开。
  • 单击“文本”字段。
  • 键盘变得可见。
  • 单击“后退”按钮。
  • 应用程序返回到第一个屏幕。
  • 单击主屏幕上的openSearchButton。
  • (iOS 10.2)屏幕在短时间内完全是白色的。
  • SearchViewController被打开。
  • 文本字段成为第一个响应器,键盘变得可见。

案例2:

  • 单击主屏幕上的openSearchButton。
  • SearchViewController被打开。
  • 单击“文本”字段。
  • 键盘变得可见。
  • 单击hideKeyboardButton
  • 键盘隐藏起来。
  • 单击“后退”按钮。
  • 应用程序返回到第一个屏幕。
  • 单击主屏幕上的openSearchButton。
  • SearchViewController被打开。

代码:

代码语言:javascript
复制
         @implementation MainViewController
        -(void) viewDidLoad {
            [super viewDidLoad];
            UIStoryboard *sb = [UIStoryboardWithName:@"Main" bundle:nil];
            self.searchView = (SearchViewController*) [sb instantiateViewControllerWithIdentifier:@"searchView"];
        }

        - (IBAction)openSearchButtonClicked:(id)sender{
            [self.navigationController pushViewController:self.searchView animated:YES];
        }
        @end

        @implementation SearchViewController
        - (void)viewWillDisappear:(BOOL)animated{
            assert(self.searchTextField != nil);    
            [self.searchTextField resignFirstResponder];
            [super viewWillDisappear:animated];
        } 

        - (IBAction)hideKeyboardButtonClicked:(id)sender{
            [self.searchTextField resignFirstResponder];
        }
        @end
EN

回答 2

Stack Overflow用户

发布于 2017-01-25 09:09:58

我已经解决了这个问题。在重新打开textFieldDidBeginEditing后,我检查了searchView的堆栈跟踪。不知何故,UIKit决定在文本字段上调用becomeFirstResponder。

我的解决方案是:在视图控制器出现之前,不允许文本字段首先响应。然而,在iOS 10.2中仍然会出现闪烁的白色屏幕。

代码语言:javascript
复制
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.allowToBecomeFirstResponser = YES;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return self.allowToBecomeFirstResponser ;
}
- (void)viewWillDisappear:(BOOL)animated{
    self.allowToBecomeFirstResponser = NO;
    [super viewWillDisappear:animated];
}     

0 ??? 0x000000011d54d91c 0x0 + 4787067164, 
1 DEMO 0x000000010c24d880 main + 0, 
2 UIKit 0x000000010d60a227 -[UITextField _becomeFirstResponder] + 481, 
3 UIKit 0x000000010d04d28d -[UIResponder becomeFirstResponder] + 324, 
4 UIKit 0x000000010cf42e03 -[UIView(Hierarchy) becomeFirstResponder] + 99, 
5 UIKit 0x000000010d609ad7 -[UITextField becomeFirstResponder] + 51, 
6 UIKit 0x000000010cf42e3b -[UIView(Hierarchy) deferredBecomeFirstResponder] + 49, 
7 UIKit 0x000000010cf43144 __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 175, 
8 UIKit 0x000000010cf43086 -[UIView(Hierarchy) _postMovedFromSuperview:] + 437, 
9 UIKit 0x000000010cf4cf4b -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1604, 
10 UIKit 0x000000010cecf392 -[_UIParallaxDimmingView didMoveToWindow] + 123, 
11 UIKit 0x000000010cf4a9a0 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 1482, 
12 UIKit 0x000000010cf4a68e -[UIView(Internal) _didMoveFromWindow:toWindow:] + 696, 
13 UIKit 0x000000010cf43112 __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 125, 
14 UIKit 0x000000010cf43086 -[UIView(Hierarchy) _postMovedFromSuperview:] + 437, 
15 UIKit 0x000000010cf4cf4b -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1604, 
16 UIKit 0x000000010cecbc38 __53-[_UINavigationParallaxTransition animateTransition:]_block_invoke + 2101, 
17 UIKit 0x000000010cf475ce +[UIView(Animation) performWithoutAnimation:] + 65, 
18 UIKit 0x000000010cecb072 -[_UINavigationParallaxTransition animateTransition:] + 1225, 
19 UIKit 0x000000010d01fe6c -[UINavigationController _startCustomTransition:] + 3038, 
20 UIKit 0x000000010d02b3fe -[UINavigationController _startDeferredTransitionIfNeeded:] + 386, 
21 UIKit 0x000000010d02bf47 -[UINavigationController __viewWillLayoutSubviews] + 43, 
22 UIKit 0x000000010d171509 -[UILayoutContainerView layoutSubviews] + 202,
票数 2
EN

Stack Overflow用户

发布于 2017-01-25 08:36:37

代码语言:javascript
复制
 -(BOOL) textFieldShouldReturn: (UITextField *) textField
 {
    [textField resignFirstResponder];
    return YES;
 }

但是,您需要将UITextFieldDelegate添加到您的SearchViewController中,以使-(BOOL) textFieldShouldReturn:在其中工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41846155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档