views:

667

answers:

2

I have a class inherited from UITableViewController and this is also the root class. This tableView contains three custom UITableViewCells (loaded from NIB file and not subclassed) and each UITableViewCell has one UITextField. Now when I implement delegate method

- (BOOL)textField:(UITextField *)textField
        shouldChangeCharactersInRange:(NSRange)range
        replacementString:(NSString *)string

I don't recieve any event. This function never gets fired, I tried implementating other delegate methods too but none of them fires. How to fire these delegate methods?

I also want to override canPerformAction:sender: function for these UITextFields (Which are part of UITableViewCell);
How to do this?

A: 

The textField:shouldChangeCharactersInRange:replacementString: method is part of the UITextFieldDelegate protocol, so you have to set a delegate for these UITextFields.

For example, if you were creating the UITextFields in your table view contoller code, and assuming the method was implemented in the same contoller, you could do this:

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(x,y,w,h)];
[textField setDelegate:self];

However, you say you're loading the UITableViewCells from XIB files. You have to somehow access the UITextFields from inside the code and call the setDelegate: method on them. You can do this by using UIView's subviews property. For example:

UIViewController *controller = [[UIViewController alloc] initWithNibName:@"foo" bundle:nil];
UITextField *textField = (UITextField *)[[controller.view subviews] objectAtIndex:0];

Though creating the UITextFields in the code is much easier and more elegant, IMHO.

As for overriding the canPerformAction:sender: method, you'll have to subclass UITextField for that.

Can Berk Güder
Thanks Can Berk for your tip, it's really helpful. How will i find the index value to pass in objectAtIndex? Here you are showing passing the value as 0 will it always be 0? As i've 3 controls on tableviewcell, label, textfield and one button. Sorry if it is basic question but i am new to iphone programming.
iamiPhone
It depends on the order in which you add the subviews, but since you're adding the subviews in the XIB file, I guess you'll have to try 0 through 2. As I said, creating the UITableViewCells in code, rather than in Interface Builder, is a better practice. Try to do that if you can, it will be a good exercise.
Can Berk Güder
A: 

Is this class a UITextFieldDelegate ? You need to provide more information, source etc.... The fact that you are not getting any event's makes me thing the problem is something missing in the NIB's. I don't use IB or NIB's for anything for this exact reason, debugging problems becomes really hard. Perhaps post your XML's

daniel
RootViewController is delegate of UITextFieldDelegate, I am unable to post my xml file here as it crosses 600 characters. How to upload xml file?
iamiPhone