» 首頁 » 討論區 » iOS程式討論 »鍵盤事件 - 使用NSNotificationCenter

鍵盤事件 - 使用NSNotificationCenter

發表人: Seachaos
積分: 2432
發表時間: 2012-01-25 01:38:33
iOS中有NSNotificationCenter可以接收Notify
以鍵盤事件的範例如下:

在viewWillAppear中註冊Notification
[sea:javaCode]
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
[/sea]

記得在viewWillDisappear中移除
[sea:javaCode]
[[NSNotificationCenter defaultCenter] removeObserver:self];
[/sea]

Actions:

[sea:javaCode]
-(void)keyboardDidShow:(NSNotification*)notif{
NSLog(@"keyboard show");
}

-(void)keyboardDidHide:(NSNotification *)notif{
NSLog(@"keyboard hide");
}
[/sea]

同步發表於 程式植物園