2017-08-24 42 views
-1

“隐藏”按钮,监听器我用的iOS:在键盘

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField 

来清除键盘上时,返回或做按钮,用户点击它完美的作品。我的问题是,当我的应用程序处于横向模式或当我在iPad中运行应用程序时,“隐藏”按钮将添加到键盘中(图片中显示的按钮)。当我点击它时,键盘被隐藏,但textFieldShouldReturn从不被调用。

如何检测何时点击此按钮?

enter image description here

回答

1

接收键盘通知 当显示或隐藏键盘,iOS的发出以下通知到任何注册的观察者:

  • UIKeyboardWillShowNotification
  • UIKeyboardDidShowNotification
  • UIKeyboardWillHideNotification
  • UIKeyboardDidHideNotification

你可以从Apple document

获得详细信息e.g

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardhideHandle:) 
              name:UIKeyboardWillHideNotification 
              object:nil]; 



- (void) keyboardhideHandle:(NSNotification *)notification { 
    NSLog(@"you received the action here"); 
} 
1

要检测时,从一个的UITextField键盘被带到了我们可以设置在viewDidLoad中的观察员,像这样的例子:

- (void)viewDidLoad { 
[super viewDidLoad]; 

// setup keyboard observers 
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardCameUp:) name:UIKeyboardWillShowNotification object:nil]; 
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWentAway:) name:UIKeyboardWillHideNotification object:nil]; 
} 

这些观察员将调用一个方法,我们班(使用@选择器)。 Mine被称为keyboardCameUp和keyboardWentAway:

- (void)keyboardCameUp:(NSNotification *)notification { 
NSLog(@"Keyboard came up!"); 
} 

- (void)keyboardWentAway:(NSNotification *)notification { 
NSLog(@"Keyboard went away!"); 
} 

src:例如http://pinkstone.co.uk