黑马程序员IOS键盘的关闭以及通知中心的运用

来源:互联网 发布:盐城seo 编辑:程序博客网 时间:2024/06/05 18:40
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
</pre><pre code_snippet_id="523183" snippet_file_name="blog_20141117_2_4904939" name="code" class="objc">#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UITextFieldDelegate,UITextViewDelegate>@end

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    NSLog(@"哈哈");    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.}- (void)viewWillAppear:(BOOL)animated{    //注册键盘出现通知    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keybardDidShow:) name:UIKeyboardDidShowNotification object:nil];    //注册键盘隐藏通知    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keybardHidShow:) name:UIKeyboardDidHideNotification object:nil];    [super viewWillAppear:animated];}- (void)viewDidDisappear:(BOOL)animated{    //解除通知    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];}-(void)keybardDidShow:(NSNotification *)notif{    NSLog(@"键盘打开");}-(void)keybardHidShow:(NSNotification *)notif{    NSLog(@"键盘关闭");}#pragma mark - UITextField Delegate Method//分类宏- (BOOL)textFieldShouldReturn:(UITextField *)textField{    [textField resignFirstResponder];    return YES;}#pragma mark - UITextField Delegate Method-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{    if ([text isEqualToString:@"\n"]) {        [textView resignFirstResponder];        return NO;    }    return YES;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

本代码是基于拖放控件来实现

键盘不能关闭的原因是需要这些控件来放弃第一响应者的身份,需要调用UIResponder类中resignFirstResponder方法,可以通过return和点击背景处罚,但是本方法是采用点击return来关闭

0 0
原创粉丝点击