IOS textView delegate - ios

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");
}

Related

iOS: listener of the "hide" button in the keyboard

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

NSNotification keyBoardWillShow and keyboardWillHide are not called

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.

Can't resize the height of uiView proportional to keyboard

When I click on textfield, execute this method keyboardDidShow and when I started to typing, the view will be go down. What is the reason for this ? Here is my code,
else if (textField.tag==2) {
[self.viewUi setFrame:CGRectMake(0,-110,320,460)];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
}
return YES;
}
- (void)keyboardDidShow:(NSNotification *)notification
{
// Assign new frame to your view
[self.viewUi setFrame:CGRectMake(0,-110,320,460)];
}
Alwin, this particular post might help you in getting this done
How to make a UITextField move up when keyboard is present?
Or trying adding the NSNotification observer at the time of loading of View Controller as in viewDidLoad.
- (void)keyboardDidShow:(NSNotification *)notification
{
// Assign new frame to your view
[self.viewUi setFrame:CGRectMake(0,-110,320,460)];
}

Reload TableView from different ViewController using Objective-C

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];
}

UIScrollView not responding to delegates

I used to have my UIView that worked fine with several delegates some of them below. Now I changed that UIView from IB to be a UIScrollView (Now used as main view).
Since I've changed to UIScrollView my event delegates such as those below dont work anymore. Such as keyboard and also I had an element that I could move around and not it is just static.
I assigned all delegates form the IB that I could think of and did most things that I know. but i am running out of ideas on why the events are not getting triggered....
if i go back to the old UIView by doing cmd + z they work.
Could anyone point me in the right direction??
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
{
[self.view endEditing:TRUE];
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
// register for keyboard notifications
return YES;
}
EDIT - Complementary answer:
#Wezly Answer is totally valid.
But if anyone doesn't want to subclass the UIScrollView and use only UITextFieldDelegate methods.
another way of doing it is adding to viewDidLoad:
Note: you still cant access many things, but its another workaround
///
/// DelegateNotifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:self.view.window];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(textFieldDidEndEditing:)
name:UITextFieldTextDidEndEditingNotification
object:self.view.window];
I suggest that you subclass your UIScrollView object and add your touch events inside of it like below..
canvasObject.h
#import <UIKit/UIKit.h>
#interface canvasObject : UIScrollView
#end
canvasObject.m
#import "canvasObject.h"
#implementation canvasObject
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.canCancelContentTouches = false;
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
{
//Do Stuff
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
return YES;
}
#end
Then link your UIScrollView using the identity inspector in interface builder to the new scroll view subclass like below..

Resources