2013-07-24 148 views
1

我知道键盘被隐藏后需要执行一些代码。方法完成后执行代码

我一直都在以块,但我只是不理解他们是如何工作的,足以做到这一点...

所有我想要做的就是运行[自hidekeyboard]然后当完成(和键盘完全隐藏),然后我想调用一个委托。

什么是处理这个问题的最好方法?

回答

1

使用NSNotificationCenter类注册为UIKeyboardDidHideNotification的监听器。

[[NSNotificationCenter defaultCenter] 
    addObserver:self 
     selector:@selector(keyboardHidden:) 
      name:UIKeyboardDidHideNorification 
     object:nil]; 

- (void)keyboardHidden:(NSNotification *)notif 
{ 
    // do stuff 
} 

(不要忘记删除观察者在- dealloc所以没有消息会被错误地发送到释放对象。)

+0

您可能还需要在willDidDisAppear可能跟removeObserver:方法。 – Geek

+0

@Aash在'-dealloc'中。 – 2013-07-24 15:25:50

+0

在大多数情况下,这将是解决方案,但我不能使用这些方法,因为它们用于其他条件。添加到这些方法的委托调用会在其他地方破坏事情,并开始变得混乱。我不得不开始与我的选择器进行交锋,我不想走这条路 – JMD

2
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyboardDidHide:) name: UIKeyboardDidHideNotification object:nil]; 

而且onKeyboardDidHide

-(void)onKeyboardDidHide:(NSNotification *)notification 
{ 
    // execute what you want. 
} 
+0

您可能还想在'willDidDisAppear:'方法中调用'removeObserver'。 – Geek