NSNotificationCenter通知基本用法(二)
阿新 • • 發佈:2019-01-06
系統自帶的通知,例如:UITextField
- (void)viewDidLoad { [super viewDidLoad]; //UIApplicationWillResignActiveNotification將要失去活動狀態的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveWillResignActiveNotification:) name:UIApplicationWillResignActiveNotification object:nil]; //UIKeyboardWillChangeFrameNotification鍵盤的frame將要發生變化時的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveKeyboardFrameChangeNotification:) name:UIKeyboardWillChangeFrameNotification object:nil]; //設定清除按鈕的現實方法,預設是不顯示。 _textField.clearButtonMode = UITextFieldViewModeWhileEditing; //UITextFieldTextDidChangeNotification textField內容發生編輯時的通知。 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveTextFieldChangeNotification:) name:UITextFieldTextDidChangeNotification object:nil]; }
- (void)receiveWillResignActiveNotification:(NSNotification *)noti { _textField.text = @""; } - (void)receiveKeyboardFrameChangeNotification:(NSNotification *)noti { NSLog(@"鍵盤noti.object %@",noti.object); //noti.userInfo傳過來的引數輸出了鍵盤的frame變化座標 NSLog(@"鍵盤noti.userInfo %@",noti.userInfo); } - (void)receiveTextFieldChangeNotification:(NSNotification *)noti { NSLog(@"文字框[noti.object text] %@",[noti.object text]); }