Question

I have 2 ViewControllers. In both i have implemented -(void) textFieldDidBeginEditing:(UITextField *)textField method. But when i CMD + ClICK the method in 2nd ViewController, it looks like it is referring to the 1st viewController instead of the delegate. Refer the image.

What is going wrong?

http://i62.tinypic.com/fon051.png

Code Below

 -(void) textFieldDidBeginEditing:(UITextField *)textField
 {
NSLog(@"in text edit %@ ",textField.text);
// UITableViewCell *cell = (UITableViewCell*) [[[textField superview]superview] superview];

CGPoint buttonPosition = [textField convertPoint:CGPointZero toView:table2];
NSIndexPath *indexPath = [table2 indexPathForRowAtPoint:buttonPosition];
UITableViewCell *cell = (UITableViewCell*)[table2 cellForRowAtIndexPath:indexPath];

UIStepper *st = (UIStepper *)[(UITableViewCell *) textField.superview viewWithTag:123];
NSString *txt=[[NSString alloc]init];
txt=textField.text;
NSLog(@"val is: %@",txt);

[st setValue:textField.text.doubleValue];

//// NSIndexPath *indexPath=[table2 indexPathForCell:cell];
[self updateStepper:st fromTetabxtField:textField];


NSInteger selectedSegment = segmentControl.selectedSegmentIndex;



if(selectedSegment == 2){
    [pobQuantity replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithInt:txt.intValue]];

    textField.text=[NSString stringWithFormat:@"%d",[[pobQuantity objectAtIndex:indexPath.row]intValue]];

    NSLog(@"pob  at Index %d is %@",indexPath.row,[pobQuantity objectAtIndex:indexPath.row]);
}

if(selectedSegment == 3){
    [brandRemindersQuantity replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithInt:txt.intValue]];

    textField.text=[NSString stringWithFormat:@"%d",[[brandRemindersQuantity objectAtIndex:indexPath.row]intValue]];
    NSLog(@"brand  at Index %d is %@",indexPath.row,[brandRemindersQuantity objectAtIndex:indexPath.row]);
}


}

Setting UItextField Delegate in CellforRowAtIndexPath method.

        UITextField *lbl1 = [[UITextField alloc] init];
        lbl1.frame = CGRectMake(400, 16, 35 ,15);
        [lbl1 setBackgroundColor:[UIColor clearColor]];
        lbl1.text = @"1";
        lbl1.delegate=self;
        [lbl1 setKeyboardType:UIKeyboardTypeNumberPad];
        [lbl1 setTag:456];
        [cell.contentView addSubview:lbl1];
Was it helpful?

Solution

In your code,

lbl1.delegate=self;

If delegate should represent class other than ITSELF, THAN its value is wrong. It should be set to some other delegate class:

lbl1.delegate = _delegateClass;

Here: _delegateClass represents some other class than itself.

for example: (psuedocode)

.h File:

@interface ViewController1 : UIViewController

@end

.m file:

@interface ViewController1 ()

@end

@implementation ViewController1

- (void)viewDidLoad
{
      // initialize view controller here
      // set _delegate to self
}

@end

.h File:

@interface ViewController2 : UIViewController
{
    // declare textfield here
}
@property id _delegate;
@end

.m file:

@interface ViewController2 ()

@end

@implementation ViewController2
@synthesize _delegate

...
...   // Here set : textfield.delegate = _delegate; // now it refers to viewController1 and all event of textfield will be handle in ViewController1
...
@end

I tried to be descriptive/ in detail as much a I can now.

Happy Coding!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top