Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :-) alt text

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.....

like image 869
Erez Avatar asked Dec 14 '10 11:12

Erez


4 Answers

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.

like image 191
Rog Avatar answered Oct 04 '22 17:10

Rog


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);
}
like image 40
user1229932 Avatar answered Oct 04 '22 18:10

user1229932


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 image 34
fofo Avatar answered Oct 04 '22 16:10

fofo


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:

enter image description here

like image 32
Jordy Avatar answered Oct 04 '22 18:10

Jordy