I have a xib file with custom view on which I have many textfields subviews. I have set delegates on each of the textfield to the file owner and successfully able to texfield delegate methods. But unfortunately the keyboard notification methods keyboardWillShow: and keyboardWillHide: not being called at all.
I added observers at textfieldShouldBeginEditing and removed the observers at textFieldDidEndEditing.
Here is the snippet of my code:
Adding Observer
-(void) textFieldShouldBeginEditing : (UITextField *) textField{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
Removing observer
-(void) textFieldDidEndEditing : (UITextField *) textField{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
//Methods for notification
- (void)keyboardWillShow:(NSNotification *)notification
{
NSLog(#"Here");
}
- (void)keyboardWillHide:(NSNotification *)notification
{
NSLog(#"There");
}
//Any of those keyboardwillShow and keyboardWillHide is not called.
It will be great if someone can help me figure out the issue with my code or any underlaying issue which I may be missing.
You are adding an observer when you are showing keyboard by editing textView. It won't call because keyboard is already shown. You should add observers on your viewWillAppear and remove viewWillDisappear methods.
Related
I'm using the
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
to remove the keyboard when the user click on the return or done button and it works perfectly. My problem is that when my application is in the landscape mode or when I run it in the iPad, the "hide" button is added in the keyboard (the button displayed in the picture). When I click it, the keyboard is hided but the textFieldShouldReturn is never called.
How can I detect when this button is tapped?
Receiving Keyboard Notifications
When the keyboard is shown or hidden, iOS sends out the following notifications to any registered observers:
UIKeyboardWillShowNotification
UIKeyboardDidShowNotification
UIKeyboardWillHideNotification
UIKeyboardDidHideNotification
you can get the detail information from Apple document
for e.g
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardhideHandle:)
name:UIKeyboardWillHideNotification
object:nil];
- (void) keyboardhideHandle:(NSNotification *)notification {
NSLog(#"you received the action here");
}
To detect when the keyboard from a UITextField is being brought up we could setup the observers in viewDidLoad, like this for example:
- (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];
}
These observers will call a method in our class (using #selector). Mine are called keyboardCameUp and keyboardWentAway:
- (void)keyboardCameUp:(NSNotification *)notification {
NSLog(#"Keyboard came up!");
}
- (void)keyboardWentAway:(NSNotification *)notification {
NSLog(#"Keyboard went away!");
}
src: e.g. http://pinkstone.co.uk
I'm not sure about how to achieve this and hence i'm asking this question.
I have 2 ViewController(VC) named abVC and xyVC.
abVC contains only TableView and xyVC contains only a button.
When i tap a button on xyVC, it should reload tableview inside abVC without moving to that abVC.
Is it possible ? If yes.. then please help me,How to do that?
You can do it with both block and NSNotification.
With block you can implement it as follows:
make a property as follows in xyzVC.h:
#property(strong,nonatomic)void(^callBack)();
Synthesize this property in xyzVC.m
#synthesize callBack
Call this block in Buttons click event
- (IBAction)btnClick:(UIButton *)sender {
if (callBack)
{
callBack();
}
}
Implement it in abcVC where you want call back:
[abVC setCallBack:^{
//reload your tableview here
}];
With NSNotification you can implement it as follows:
Register for receiving notification in abcVC.m
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reloadTableView) name:#"ReloadTable" object:nil];
- (void)reloadTableView{
// Reload your tableview here
}
Now post the notification from xyzVC
- (IBAction)btnClick:(UIButton *)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:#"ReloadTable" object:nil];
}
Note: If you are using NSNotification then don't forget to remove it when your view controller is dismissed
I hope this will help you :)
Add this line in your button action method
[[NSNotificationCenter defaultCenter] postNotificationName:#"ReloadTable" object:nil];
Add this code where you have implemented table, so in abVC:
inside viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reloadTableViewData) name:#"ReloadTable" object:nil];
Add following method
- (void)reloadTableViewData{
[self.myTableView reloadData];
}
Implement dealloc method (to prevent crash)
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"ReloadTable" object:nil];
}
my problem is that i have a textfield on my viewcontroller which should be get filled by user and to provide options to fill the textfiled there is a arrow button front of textfield. Clicking on it open a new viewcontroller with a list of options. So when the user click on any option from that viewcontroller the data will automatically fill in that textfield and user return on previous viewcontroller. And i can not use preprefor segue in it so please provide an answer. If i am missing something let me know i ll comment or edit.
You can use NSNotificationCenter to fill back your UITextField. Add Observer to your first viewController class where you want to fill the textField. Add this lines of code in viewDidLoad-
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(fillMyTextField:) name:#"fillMyTextField" object:nil];
than add selector method in same class--
- (void)fillMyTextField:(NSNotification *)notification
{
self.myTxtField.text = (NSString *)notification.userInfo;
}
Now in your other viewController class where you select data for textField. Write the below code in method where you select your data like-
- (IBAction)selectDataAndBackToPreviosVC:(UIButton*)selectedOptionBtn {
id object=[NSString stringWithFormat:#"%#",selectedOptionBtn.titleLabel.text];
[[NSNotificationCenter defaultCenter] postNotificationName:#"fillTextField" object:nil userInfo:object];
[self.navigationController popViewControllerAnimated:YES];
}
in this I have used navigationViewController which use Push and Pop viewControllers and i have some UIButton in option selection viewController. I'm passing button title which is options to myTextField on IBAction.
In class where textfield Define Below method (add notification observer)
in viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(fillTextField:)
name:#"fillTextField"
object:nil];
- (IBAction)fillTextField:(NSNotification *)sender
{
textField.text = (NSString *)notification.object
}
In Another class where u select date set below method in date selection action
[[NSNotificationCenter defaultCenter] postNotificationName:#"fillTextField" object:nil userInfo:textFieldInfo];
You can use NSNotification:
In View controller ofTextField add this
- (void) viewDidLoad
{
//Your rest of code then below statement
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveTextNotification:)
name:#"TestNotification"
object:nil];
}
- (void)receiveTextNotification:(NSNotification *)notification {
NSLog(#"%# updated", [notification userInfo]);
}
- (void) dealloc
{
// If you don't remove yourself as an observer, the Notification Center
// will continue to try and send notification objects to the deallocated
// object.
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
In View controller of Button, on button click add this:
- (IBAction)fillTextFieldAction: (id) sender {
// All instances observing the `TestNotification` will be notified
[[NSNotificationCenter defaultCenter] postNotificationName:#"TestNotification" object:self userInfo:text]; //Object here can be any changed value .
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(fillTextFieldAction:) name:#"fillTextField" object:nil];
- (IBAction)fillTextFieldAction:(NSNotification *)notification
{
textField.text = (NSString *)notification.object
}
[[NSNotificationCenter defaultCenter] postNotificationName:#"fillTextField" object:nil userInfo:textFieldInfo];
I have created a UITextView *mytextview . I want to get event when click textview before UIKeyboardWillShowNotification delegate.
Now, When I click textview, is's call UIKeyboardWillShowNotification. After that, it's call - (void)textViewDidBeginEditing:(UITextView *)textView, it's not good!
I want to mytextview delegate was called before call UIKeyboardWillShowNotification
How to I can resolve this problem
Use textViewShouldBeginEditing provided by the UITextViewDelegate, call your delegate method in it, and return YES from this method to let user edit the text.
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
[yourDelegate yourMethod:textView];
return YES;
}
The sequence of Event execution are
UIKeyboardWillShowNotification
textViewDidBeginEditing
Now as per our requirement I Fire On Notification that is called when KeyBoardWillShow.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(textViewDidBeginEditing:)name:UIKeyboardWillShowNotification object:nil]
And in the selector I passed same function which is called after UIKeyBoardWillShown, Also you can set your Own Function, which you want to do Before UIKeyBoardShow
This is My Code
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(textViewDidBeginEditing:) name:UIKeyboardWillShowNotification object:nil];
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
NSLog(#"textViewDidBeginEditing");
}
I have a UIView which contains UITexView and a Button.
I have a delegate UITextViewDelegate.
When I first set the cursor on the UITextView, the delegate function "textViewShouldEndEditing" gets called, and this function triggers the UIKeyboardWillShowNotification notification. So far so good.
When I click on the button, I call the function [self.textView resignFirstResponder];, this function then calls the delegate "textViewShouldEndEditing", but the notification UIKeyboardWillHideNotification never gets called.
I have a listener for the notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(_keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
Is there anything missing ???
[self.view endEditing:YES]; is the trick, Thank you all for your help
you should post a notification to the notification center in the following way
[[NSNotificationCenter defaultCenter]
postNotificationName:#"TestNotification"
object:self];
and you can check whether the notification has received or not in the following way
if ([[notification name] isEqualToString:#"TestNotification"])
NSLog (#"Successfully received the test notification!");
Is your code is like this, check and let me know
-(void)viewDidAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
-(void) keyboardWillHide:(NSNotification *)note{
NSLog(#"test");
}