In Objective-C where is textfielddoneediting originally declared? - iphone

I've performed a pretty exhaustive search of the documentation namely: UIViewController, UITextField, and UITextFieldDelegate, but I can't figure out where the method "textFieldDoneEditing" is originally declared.
I know that I have to use it in my ViewController to get the keypad to disappear in this manner:
- (IBAction)textFieldDoneEditing:(id)sender
{
[sender resignFirstResponder];
}
But is method part of a protocol that is inherently implemented by UIViewController?
Thanks

Without seeing the other code, I suspect this was connected to the textfield via interface builder connecting to the Editing Did End event. There is no textFieldDoneEditing method in any of UITextField parent classes.

Related

How does a delegate method know when to be called

I'm just wondering how exactly does a delegate method know when to be called? For example in the UITextFieldDelegate protocol the textFieldDidBeginEditing: method is called when editing begins in the textfield (provided I implemented this method).
So how exactly does the code to detect when to call textFieldDidBeginEditing:? Does the system just check if textFieldDidBeginEditing: is already implemented and if it is it runs that method? Is there something under the hood that I'm not seeing?
Exactly.
I can't vouch for how Apple's framework code is implemented under the hood, but an exceedingly common refrain is:
if ([[self delegate] respondsToSelector:#selector(someInstance:didDoSomethingWith:)]) {
[[self delegate] someInstance:self didDoSomethingWith:foo];
}
This allows you to have optional delegate methods, which appears to be your question.
The code doesn't 'detect when to call' a delegate method. The textField receives an event, and calls the method on it's delegate (which has the textFieldDidBeginEditing: method implemented).
In short, when you tap the textfield to start editing, the textField says 'oh, I'm editing now!' and internally calls [self.delegate textFieldDidBeginEditing:self], where the delegate is the instance in which you've set to be the delegate (usually a UIViewController subclass)

backgroundTouched not working in uitableviewcontroller iphone

my code:
.h:
- (IBAction)backgroundTouched:(id)sender;
.m
- (IBAction)backgroundTouched:(id)sender {
[textField resignFirstResponder];
[self.view endEditing:YES];
NSLog(#"backgound taped");
}
I dont know why the backgroundTouched not called when i tap the background and the keyboard not hidden. I think never called because of wiring up code problem.
Neither Both textField resignFirstResponder and self.view endEditing:YES nor NSLog is working.
Can anyone let me know how to do this? or What am i missing here?
Im trying to hide the keyboard after done writing in uitextfield, the textfield is inside uitableview cell.
P.S i made it in uitableviewcontroller without xib file
Thank you.
Instead of a backgroundTouched method (which I have no idea how it gets actually called), consider using the delegate method for UITextField, namely textFieldDidEndEditing: (I've linked Apple's documentation for you).

How to identify UITextFields and Keyboard Activity?

I've searched in the web but didn't found anything similar to what I want. So,I am creating an application and I need to recognize when the user leaves a specific UITextField,more clearly.when the user enters a value in the UITextField and after touch outside to dismiss the keyboard, I need to recognize that the UITextField has lost activity for,after I perform an action.
Is this possible?
Look up UITextFieldDelegate in the Apple docs. Specifically the methods textFieldDidEndEditing: and textFieldShouldReturn:. Hook up the specific UITextField to an outlet and assign its delegate to your viewController. Then in the delegate method, if you need to make sure it's a specific text field, compare it to the IBOutlet.
write UITextFieldDelegate in .h file then after include the following method in .m file.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return NO;
}

How to access variables of a ViewController in a subclass?

I guess this is basic, but I can't get my head around this.
I used to have only one ViewController in which all my variables were defined, e.g. an UITextView named myTextView. I also had methods in this ViewController for handling events that relate to myTextView, such as - ()hideKeyboard { // do something with myTextView or - (void)keyboardWillShow:(NSNotification *)notification { // do something with myTextView.
As my program became bigger and bigger, I thought about using subclasses, especially for other views. So I started a subclass, eg. mySubClass.h and mySubClass.m, in which I had another UITextView (for argument's sake myOtherTextView). In order to incorporate mySubClass, I #imported it into my ViewController and added a #class mySubClass; and could then produce instances of this class so as to use it in my App.
So far so good. As you can imagine, all the nice methods I defined in my ViewController for what should happen when an UITextView is edited (such as hiding keyboard etc.) didn't work for the UITextView in mySubClass.
It was then suggested to me that I should make another class in which I had all the keyboard events and subclass my ViewController and mySubView to it:
#interface ViewController : MyKeyboardEventsViewController
Now, the problem I am seeing is that I won't be able to access all the views, textviews, textfields etc. that I have created in my ViewController (e.g. myTextView which I mentioned earlier).
How can I achieve that all the variables that I have defined in my ViewController will also be available for MyKeyboardEventsViewController? Or is there another way to handle this?
Basically, I don't get how MyKeyboardEventsViewController will be able to access variables in my ViewController which it will need (e.g. the UITextView in question, or the accessoryView which will pop up etc. etc.).
Any suggestions would be very much welcome.
Example:
Class A contains a ivar UITextField textField
Class B subclasses Class A and thus it already contains ivar textField
Note: it's not the other way around. Class A does not "see" what ever is created in Class B.
When ever you subclass a class you give your new class the same ivars end methods of that subclassed class.
I hope this is what you were asking for.
EDIT
So for your example I would do the follwing:
Create a class "MyUIKeybordEventResponder"
Implement all the responder methods like - (BOOL)textFieldShouldReturn:(UITextField *)textField
Subclass your ViewController from "MyUIKeybordEventResponder"
Note method textFieldSHouldReturn has a parameter UITextField so it knows which textfield was pressed. So in a way it receives your textField from the subclass.
If I'm understanding this correctly, you have a UIViewController with MyKeyboardEventsViewController as an instance variable and you want to communicate between the two? If that is the case, one option would be to create a protocol.
#protocol MyKeyboardDelegate
- (void)closeAccessoryView;
#end
(Note - make whatever methods in the protocol that you need, this is simply an example)
In your MyKeyboardEventsViewController you then include the protocol file, and create an ivar
id <MyKeyboardDelegate> delegate;
Also make it a property and synthesize it.
Whatever class that is going to create the keyboardviewcontroller should delcare themselves as conforming to the protocol.
#interface MyViewController : UIViewController <MyKeyboardDelegate>
...
#end
When you create the MyKeyboardEventsViewController, set the delegate.
MyKeyboardEventsViewController *eventsVC = [[MyKeyboardEventsViewController alloc] init];
[eventsVC setDelegate:self];
Now just implement the delegate method and perform whatever action that is necessary.

How to react on the DONE button of the keyboard when using a UITextView?

UITextView doesn't inherit from UIControl, so there are not addTarget:forControlEvent: methods possible. I used to register an action method just to resign first responder when UIControlEventEditingDidEndOnExit happens.
How can I do that with an UITextView? Is there some delegate + protocol I must implement so I can make the keyboard go away?
Yes UITextView has a delegate protocol you should implement. The delegate protocol is naturally named UITextViewDelegate. You should notice by browsing the documentation that there is a pattern; For any class Foo that has a delegate protocol the protocol is always named FooDelegate, there are no exception.
Now you can as James pointed out intercept text changes using the UITextViews delegate and dismiss the keyboard. I would however discourage you from doing so, because it violates the Human Interface Guidelines. A UITextView is intended for editing text with multiple lines of text, intercepting the enter key to mean something elsa than line breaks is not advised.
If you want a text field with only a single line of text, or being dismissed by enter key, then you should instead use a UITextField, which also have a delegate protocol UITextFieldDelegate. This protocol has a method intended for what you want:
-(BOOL)textFieldShouldReturn:(UITextField*)textField {
[textField resignFirstResponder];
return NO;
}