Weird interaction between UITextField textFieldShouldEndEditing and leftView - ios

I'm finding a weird behaviour when I add a leftView (or righView) to a UITextField, then return NO to textFieldShouldEndEditing and dealocate the view controller, when trying to access the view controller again (a new instance, one would think), some times I get an EXC_BAD_ACCESS, and other times the text field is simply unresponsive.
I have no idea why this is happening, so I'm posting below the steps to reproduce.
Start with a very simple project with a Navigation Controller and 2 View Controllers, the first one has just a button to go to the second one, and the second one just have a UITextField.
The UIViewController of this second controller is the UITextField delegate:
#interface ViewController () <UITextFieldDelegate>
#property (nonatomic, weak) IBOutlet UITextField *textField;
#end
#implementation ViewController
- (void)dealloc {
NSLog(#"Dealocating View Controller");
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
textField.leftViewMode = UITextFieldViewModeAlways;
return NO;
}
#end
Now go to the second view controller, you can edit the text field with no problem, but cannot stop editing (as expected), but you CAN go back to the first view controller (at this point the second view controller is dealocated, as testified on the console).
But now if you again go into the second view controller (a new instance, I would expect, since the other one was deallocated), you'll find you can no longer interact with the text field.
Why is this happening? What am I doing wrong?
Notes
If I return YES to textFieldShouldEndEditing, everything works fine; but I want to keep the user on the text field until they enter a valid text.
If I don't add a leftView, everything works fine; but I want to give the user a visual feedback as to why the text field is not allowing them to leave.
Edit in response to Teja Nandamuri
I updated the view controller code to try and make sure the textField resigned first responder before the view controller get deallocated, but even with these changes, upon reentering the textField is unresponsive:
#interface ViewController () <UITextFieldDelegate>
#property (nonatomic, weak) IBOutlet UITextField *textField;
#end
#implementation ViewController
- (void)dealloc {
NSLog(#"Dealocating View Controller");
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
if (textField == self.textField && ![textField.text isEqualToString:#"bien"]) {
textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
textField.leftViewMode = UITextFieldViewModeAlways;
return NO;
}
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
}
}
#end

Please note that even you return NO, the UIKit will end the editing while leaving from the second controller.
Consider this case, you return NO to prevent the user moving to another control, this works as long as you stay in the same controller. If you move away from it, everything gets deallocated and when you comeback again, the textField will get instantiated again, and UIkit thinks the new textfield is a different one and will not allow the new textField to becomeFirstResponder, since the old one did not resign its responder. So you cannot edit the new textField.
Adding the leftView will not make a difference here.The only thing matter is the delegate method to return YES .Because you cannot control UIkit, and it expects you to return YES, so modify your delegate method:
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
if (your-condition) {
textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
textField.leftViewMode = UITextFieldViewModeAlways;
return NO;
}
return YES;
}
This case works as long as you are in the second View controller. Anyway the problem still exists if you leave from the second controller, and the delegate method returns NO, you should stop user from going back to first view controller if your text field condition is not met.

Related

How can I make a UITextField the first responder, correctly? (Tried 3 methods)

Here's the setup:
SNController is an NSObject and it has inside:
UIView
UITextField
UISegmentedControl
UILabel
UIButton
The view is the same as the view inside the ViewController.
One can be visible at the time. When the UITextField is visible (alpha = 1.0)
all the others are invisible (alpha = 0.0).
(1) I perform an animation and I want the text field:
UITextField *textField;
to become the first responder while the animation happens:
[textField becomeFirstResponder];
[UIView animateWithDuration: ...];
Unfortunately it doesn't work.
I've logged the text field and it showed that it cannot become the first responder and I have no idea why...
I've tried another two methods but none of them worked so far:
(2) A complicated way to communicate with the ViewController from the SNController:
Inside the SNController.h/.m:
#property (nonatomic, copy) void(^eventCallBack)();
if (self.eventCallBack) {
self.eventCallBack();
}
Inside the ViewController.m:
__weak ViewController *self_ = self;
self.restoration.controller.eventCallBack = ^(){
[self_.restoration.controller.textField becomeFirstResponder];
};
(3) I also tried these methods from inside the SNController:
[self.textField performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
[self.textField performSelectorOnMainThread:#selector(becomeFirstResponder) withObject:nil waitUntilDone:YES];
It just won't give up, it doesn't become the first responder no matter what.
Any ideas?
I realised there was a mistake. There isn't a UITextField inside the SNController but a UIView subclass named SNTextField.
I implemented this method where the UITextField was initialised:
- (void)activateKeyboard
{
[self.textField becomeFirstResponder];
}
and it worked!

Should [UIView endEditing:YES] ever return no?

I am having a hell of a time fixing a bug, which currently is only occurring when I run my app on the simulator. Essentially, I have a UITextView that I am trying to send the message endEditing to. If I send the message while the user is editing the textview (forced or otherwise) I get back YES. If however, the user has not yet begun editing the textview and I send the message endEditing:YES, I get back NO. Should this even be possible? Shouldn't endEditing:YES always force the view to end editing?
Additional Details: I have tried setting the owning class to be the uitextviews delegate, but even then it doesn't look like the shouldEndEditing method even gets called.
UPDATE: It does not seem that this is normal behavior (that the method should return no if the text field is not currently the first responder).
I created a simple test:
AppDelegate.h
#import <UIKit/UIKit.h>
#interface DRAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (assign) IBOutlet UITextView *textView;
#property (assign) IBOutlet UIButton *endButton;
#property (assign) IBOutlet UILabel *results;
-(IBAction)tapEndEditingButton:(id)sender;
#end
and AppDelegate.m
-(void)tapEndEditingButton:(id)sender
{
BOOL didEndEditing = [self.window endEditing:YES];
NSString *result = (didEndEditing) ? #"YES" : #"NO";
_results.text = result;
}
Regardless of whether the textfield has focus, and regardless of whether I set the force parameter of endEditing to YES or NO, didEndEditing gets set to YES.
Check Apple Doc on UIView
endEditing:
Causes the view (or one of its embedded text fields) to resign the first responder status.
...
Return Value
YES if the view resigned the first responder status or NO if it did
not.
Discussion
This method looks at the current view and its subview hierarchy for
the text field that is currently the first responder. If it finds one,
it asks that text field to resign as first responder. If the force
parameter is set to YES, the text field is never even asked; it is
forced to resign.
So... When you said:
If however, the user has not yet begun editing the textview and I send the message endEditing:YES, I get back NO
It's perfectly fine because in your scenario, since there is no firstResponder to resign, calling -endEditing: will return NO and doesn't harm the performance (isn't a bug either, imho)
To answer the essence of the question:
[UIView endEditing:YES/NO]; will return NO if the specified UIView object was not the firstResponder.
Example:
-(void)testEndEditing
{
UIButton *btnTest = [UIButton buttonWithType:UIButtonTypeCustom];
[btnTest setFrame:CGRectMake(0, 130, 320, 44)];
[btnTest setBackgroundColor:[UIColor blueColor]];
[btnTest addTarget:self action:#selector(myEndEditing:)
forControlEvents:UIControlEventTouchUpInside];
[btnTest setTitle:#"End Editing" forState:UIControlStateNormal];
[self.view addSubview:btnTest];
UITextField *txtFTest = [[UITextField alloc] init];
[txtFTest setFrame:CGRectMake(0, 50, 320, 30)];
[txtFTest setBackgroundColor:[UIColor orangeColor]];
[txtFTest setText:#"textField"];
[self.view addSubview:txtFTest];
/*
globally declare "UITextView *txtVTest;"
*/
txtVTest = [[UITextView alloc] init];
[txtVTest setFrame:CGRectMake(0, 80, 320, 50)];
[txtVTest setBackgroundColor:[UIColor redColor]];
[txtVTest setText:#"textView"];
[self.view addSubview:txtVTest];
//make UITextField object the firstResponder
[txtFTest becomeFirstResponder];
}
-(void)myEndEditing:(UIButton *)sender
{
//test endEditing method on UITextView object
BOOL isWhat = [txtVTest endEditing:YES];
NSLog(#"%i",isWhat);
}
PS: If neither the textField nor the textView is the firstResponder then it returns YES (dunno why but i'll check)

How do I dismiss a keyboard after my user has entered the value for a text field?

I know this question has been asked a few times, but none of the answers have enough detail for me (me = n00b) to understand.
I have a simple little app that I put together via the storyboard, it has two text fields that the user can type into. I want the user to be able to dismiss the keyboard after they edit either field.
I know it has something to do with "resignFirstResponder" but I'm not sure where to put that.
Here is my top secret code
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize thing1;
#synthesize thing2;
// NSArray things = #[thing1, thing2];
// self.thingLabel.text = array[arc4random()%array.count];
- (IBAction)pick:(id)sender {
// create an empty list that will get filled with things
NSMutableArray *things = [NSMutableArray arrayWithObjects:thing1.text,thing2.text, nil];
NSString *theThing = things[arc4random()%things.count];
NSLog(#"the thing is %#", theThing);
}
#end
and here is my ViewController.h file:
//
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UITextField *thing1;
#property (weak, nonatomic) IBOutlet UITextField *thing2;
#end
Since your view controller adopts the UITextFieldDelegate protocol (i.e., <UITextFieldDelegate>), then the easiest technique is to have the view controller implement the following two methods:
// place in ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// note that you could set the text field delegate in IB instead
self.thing1.delegate = self;
self.thing2.delegate = self;
}
// this method gets called by the system automatically when the user taps the keyboard's "Done" button
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
[theTextField resignFirstResponder];
return YES;
}
That's all the code you need. Now when the user taps the keyboard's "Done" button, the keyboard goes away.
You could just add a button beside the textfield and dismiss it when that one is pressed:
UIButton *doneEnteringButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[doneEnteringButton addTarget:addTarget:self action:#selector(dismissKeyboard:) forControlEvents:UIControlEventTouchUpInside];
- (void)dismissKeyboard
{
[self.view endEditing:YES];
}
This single button will dismiss the keyboard, no matter which textfield you are in. If you don't want the button a cool way would be to add a gesture recognizer to the parent view of the textfield and do endEditing:YES there.
Hope this helps! Ask if it doesn't
try this code, when user tap any where to dismiss keyboard
- (void)setupKeyboardDismiss{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
UITapGestureRecognizer *singleTapGR =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(tapAnywhereToDismissKeyboard:)];
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[nc addObserverForName:UIKeyboardWillShowNotification
object:nil
queue:mainQueue
usingBlock:^(NSNotification *note){
[self.view addGestureRecognizer:singleTapGR];
}];
[nc addObserverForName:UIKeyboardWillHideNotification
object:nil
queue:mainQueue
usingBlock:^(NSNotification *note){
[self.view removeGestureRecognizer:singleTapGR];
}];
}
- (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer{
[self.view endEditing:YES];
}
This depends on the UI & functionality you are trying to develop. You can do it in following ways:
Add a "Done" button to your UI for user to take an action once they are done with entering data in text fields and then in the action handler for that button call [yourTextField resignFirstResponder].
Add a custom keyboard down button to your keyboard.
Implement the below delegate methods on UITextField and put logic in them and call [yourTextField resignFirstResponder] once the required criterion is met. For example once user has entered certain number of characters it will auto-dismiss the keyboard.
When user is done and tap outside the textfield view, dismiss the keyboard. Implement 'touchesbegan' on UIView and let the viewcontroller about it.
-(BOOL)textField:(UITextField *)iTextField shouldChangeCharactersInRange:(NSRange)iRange replacementString:(NSString *)iString
-(void)textFieldDidEndEditing:(UITextField *)iTextField

Dismiss the keyboard with MULTIPLE UITextFields?

Is it possible to dismiss the keyboard when you have MULTIPLE UITextFields ? If so how ?
As a side note, do I have to dismiss the keyboard for Each and Every field or can it be done globally ? Oh and it would be super cool if I don't have to touch the DONE button, I'd ideally like a solution that where the user touches anything BUT the field in question and the keyboard automagically disappears...
Oh and if you'd be so kind step by step instructions.
I should have added that I have a method already to resign the keyboard....
However, it only runs when my form is submitted! (see method below)
My question is how to the keyboard to hide/dismiss without having to jump thru so many damned hoops! You'd figure after 6 years, a mature operating system would have a way to GLOBALLY hide the keyboard....NOT!
Ok, enough whining....
- (void)hideKeyboard {
[self.dancePlace resignFirstResponder];
[self.danceGate resignFirstResponder];
[self.danceTerminal resignFirstResponder];
[self.danceText resignFirstResponder];
[self.danceDate resignFirstResponder];
[self.danceStyle resignFirstResponder];
[self.danceTimeOut resignFirstResponder];
}
And this is called when my button is submitted....
- (IBAction)addListingPressed:(id)sender {
// NSLog(#"BUTTON PRESSED");
[self hideKeyboard];
[self valuesAdded];
}
My question, assuming anyone can answer this...and I suspect not, is there a way to globally hide the keyboard if the following conditions are MET: 1.) the user taps OUT of any one of the existing fields, 2.) presses anywhere else on the screen. 3.) Is no more than a line or two in the existing viewcontroller.m file. 4.) I don't have to add a confusing button on the viewcontroller. (any time I have to add outlets, the damned thing is crashing on me...and then nastiness happens, and really...remember I am JUST a beginner, and its very confusing to read that I have to place this here and that there...oy. Simple folks, simple. I'm not looking for elegant solution, just so that it works.
I have a super class that all my view controllers inherit from. In that class I have this code.
MySuperViewController.h
#import <UIKit/UIKit.h>
#interface MySuperViewController : UIViewController
#property(strong, nonatomic) UITapGestureRecognizer *backgroundTapGestureRecognizer;
#end
MySuperViewController.m
- (void)viewDidLoad{
//add a tap gesture recognizer to capture all tap events
//this will include tap events when a user clicks off of a textfield
self.backgroundTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(onBackgroundTap:)];
self.backgroundTapGestureRecognizer.numberOfTapsRequired = 1;
self.backgroundTapGestureRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:self.backgroundTapGestureRecognizer];
}
- (void)onBackgroundTap:(id)sender{
//when the tap gesture recognizer gets an event, it calls endEditing on the view controller's view
//this should dismiss the keyboard
[[self view] endEditing:YES];
}
I have the UITapGestureRecognizer as a public property, so I can override it if I need to.
subclass
MyViewController.h
#import <UIKit/UIKit.h>
#import "MySuperViewController.h"
#interface MyViewController : MySuperViewController<UIGestureRecognizerDelegate>
#end
MyViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//You don't always want the keyboard to be dismissed, so you tie into the gesture recognizer's delegate method
//By doing this, you can stop the endEditing call from being made
[self.backgroundTapGestureRecognizer setDelegate:self];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
//touch.view is the view that recieved the touch
//if this view is another textfield or maybe a button, you can return NO and the endEditing call won't be made
if (touch.view == self.myViewThatShouldNotBeBlocked) {
return NO;
}
//if you want the gesture recognizer to accept the event, return yest
return YES;
}
I uploaded an example project to github.
https://github.com/JeffRegan/KeyboardBeGone
RDVKeyboardAvoiding is a scroll view with a tap gesture recognizer, designed for multiple textViews/textFields. It keeps track of the active view and removes a lot of boilerplate code.
tap anywhere outside the textField .. it will hide it..
[self.view endEditing:YES];
There are couple of other ways to do it.
[myEditField resignFirstResponder];
[myEditField endEditing];
[parentView endEditing];
If you dont wont to do so many things and simply want to dismiss keyboard than give iboutlet to each of your text filed to following method..
-(IBAction)hidekeyboard:(id)sender
{
[sender resignFirstResponder];
}
Yes, you only have to dismiss it for the one that is currently being edited.
In order to know which one is being edited, you can check the -(BOOL)isFirstResponder property, which will return YES if it is the first responder (the one being edited) or NO if it is not. Once you know which one is the first responder you can call -(void)resignFirstResponder on that one to get rid of the keyboard.
For example, if you have a method called -(void)aMethod that you want to dismiss the current view controller and you have an array of textViews called textArray, you could do a little loop such as:
-(void)aMethod {
for (UITextField *text in self.textArray) {
if ([text isFirstResponder]) [text resignFirstResponder];
return;
}
}
This way, you can have a variable number of textFields and it will still work.
If you only have one or two textFields and you do not want to create an Array object, you could do (assuming the fields are named text1 and text2:
-(void)aMethod {
if ([text1 isFirstResponder]) [text1 resignFirstResponder];
else if([text2 isFirstResponder]) [text2 resignFirstResponder];
}
Also, to make things easier for the future you could create a category method for UIView (which is what I do) to get the current first responder if it exists as a subview of that view:
#implementation UIView (GetFirstResponder)
- (UIView *)getFirstResponder {
if ([self isFirstResponder]) return self;
else {
for (UIView *subview in self.subviews) {
UIView *firstResponder = [subview getFirstResponder];
if (firstResponder) return firstResponder;
}
}
return nil;
}
You can put this method on the top of any file that you want to call it from, or create a separate file for it and import it.
Once you have this method, you can call:
- (void)aMethod {
UIView *view = [self.view getFirstResponder];
if (view) [view resignFirstResponder];
}
[superview endEditing:YES]; // superview can be the view controller's view property.

Delegate for UITextField not working...Return button not responding

I've just started with xcode and objective-c and did some very basic apps, but what i'm having problem with is very basic this. the keyboard return button not hiding the keyboard.
I've searched the internet for the solution and all they say is to connect delegate to the file's owner and add the function and it should work, i did that and nothing is working.
I have an ok button and it is working and also clicking on any free space on the screen is working, just the return button....
I am using the simulator, not testing on iphone yet. (xcode 3.2.5 64 bit with the 4.2 simulator).
This is the line of code that should connect the delegate to every textFiled.
1. i've tried already to return both YES and NO, didn't work.
2. i've tried both a specific object name for the textField and this general way, didn't work.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
In the: basic view controller connection -> connections -> outlets, i have the: delegate -- File's Owner. and in the file's owner in referencing outlets there is: delegate - Round style text.....
EDIT - i forgot to mention before, i've check and the method isn't being called!!!
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(#"Working!!!");
[textField resignFirstResponder];
return YES;
}
what should i do to make it happen? that is why people say to connect the delegate, but in my case it is connected and not triggering the function...i know it is kind of dumb question but for a nobie like me the solution is not obvious...
OK, another Edit - with all my code: just can't understand what to do....
This is: basicViewController.h:
#import <UIKit/UIKit.h>
#interface basicViewController : <#superclass#> <UITextFieldDelegate>
#interface basicViewController : UIViewController <UITextFieldDelegate> {
//every object that we want to interact with (like text field or lable) is call an outlet!!!!
//here we define the outlets for our program
IBOutlet UITextField *txtName;
IBOutlet UILabel *lblMessage;
}
//here are the getters and setter for our outlets
#property (nonatomic, retain) IBOutlet UITextField *txtName;
#property (nonatomic, retain) IBOutlet UILabel *lblMessage;
//method decleration for the OK button action
- (IBAction) doSomething;
//method for hiding the keyboard when clicking on empty area in the app
//we will put an invisible button on all area and clicking on it will make keyboard disapear
- (IBAction) makeKeyboardGoAway;
#end
This is basicViewController.m:
#import "basicViewController.h"
#implementation basicViewController
//synthesizeing the objects that we made' this will create the getter and setters automaticly
#synthesize txtName;
#synthesize lblMessage;
- (IBAction) doSomething{
// makeing keyboard disapear when pressing ok button (doing that form the text field)
//when pressing the OK button, the keyboard will disapear and when clicking in the text field it will show again
[txtName resignFirstResponder];
NSString *msg = [[NSString alloc] initWithFormat:#"Hello, %#",txtName.text];
//the objective-c way for setting the test in the text field
[lblMessage setText:msg];
//the regular object oriented way
//lblMessage.text = msg;
[msg release];
}
- (IBAction) makeKeyboardGoAway{
[txtName resignFirstResponder];
}
//when clicking the return button in the keybaord
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(#"Working!!!");
[textField resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
Maybe now i am more clear, sorry i didn't do it before.
Any one has an idea what am i doing wrong? it should be pretty strait forward.....
EDIT - Adding an image of all the elements, i hope that will help to help me :-)
10x a lot for every one that is trying to help....i really like this framework, it is so great after c++ and java, python and many other...and i am working with a book, but it is for ios 3.1, maybe that is the problem.....
Firstly you should check if textFieldShouldReturn: is actually being called by adding an NSLog statement or breakpoint at the beginning of the method.
Once that's out of the way, try an manually declare that your view controller conforms to <UITextFieldDelegate> protocol in your interface file:
#interface YourClass : ... <UITextFieldDelegate>
Also declare a property & outlet for your UITextField, make the appropriate connections in IB and manually declare self as the UITextField delegate with:
self.yourUITextFieldObject.delegate = self;
Once that's done see if your method above is now being called and make sure you return YES.
Just write one line in the
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
}
before return YES;
the final version will be as given below:
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(#"%#",textField.text);
}
You need to assign the delegate of the textfields to your file owner. The textfields are sending the message, but doesn't have a delegate to respond to it.
Use the interface builder to do that.
You have to implement this method..
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Like Rog said, don't forget to register the textfield to the delegate, you can do this manually as he said but in Storyboard you can just control drag from all of your textfields to the view controller and register the delegate (choose delegate). Only the textfields that are registered can make use of all those methods.
So this line is important:
self.yourUITextFieldObject.delegate = self;
Or even more easy these days is to just use the storyboard:
textfield is in a subview? in this case, make sure textfield have as delegate the FileOwner.
put a log at the
- (IBAction) makeKeyboardGoAway
function. I think its this method everytime anything is tapped on the screen. In that case, you will need to send the touch event to the text field. Not sure how this is done but that should do it.
Else try removing the which takes care of tap(click) all over the view and try to do what you are doing.
Most likely the problem is that your actual view controller in the running application is not a "basicViewController" but a UIViewController that does not implement the UITextFieldDelegate-protocol.
What you've done in the interface builder by selecting your class "basicViewController" as the FilesOwner is just declaring the FilesOwner-object in your running application to be of type basicViewController; the actual object is not instantiated by this declaration and in your case it is not in the xib / nib.
Some other part of your code actually instantiates a view controller object and loads the xib / nib file. At that place, I guess your code is instantiating a UIViewController (typically by auto-generated code) and not an instance of your basicViewController; you simply have to change the class there.
Furthermore, this error often happens when using a UINavigationController or UITabBarController in the Interface Builder that is (should be) configured to instantiate and load other custom views. If you use such a higher-level controller, double-check that it is actually configured to use your basicViewController, not UIViewController when loading your view from the xib / nib.
Hope, that solves the issue!
Can you try this..
#interface ClassName : SuperClass < UITextFieldDelegate >
Use like this...
textfield.delegate=self;
and use the UITextFieldDelegate in .h class
You can always dismiss the keyboard when you don’t even know which view the text field is in by using:
Objective-C:
[[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder)
to:nil
from:nil
forEvent:nil];
Swift:
UIApplication.sharedApplication().sendAction("resignFirstResponder",
to:nil,
from:nil,
forEvent:nil)

Resources