Cmd + click方法指向不同的ViewController

时间:2014-01-29 10:13:21

标签: ios uiviewcontroller uitextfielddelegate

我有2个ViewControllers。我已经实现了-(void) textFieldDidBeginEditing:(UITextField *)textField方法。但是当我在第二个ViewController中CMD + Click方法时,看起来它指的是第一个viewController而不是委托。参考图片。

出了什么问题?

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

以下代码

 -(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]);
}


}

在CellforRowAtIndexPath方法中设置UItextField委托。

        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];

1 个答案:

答案 0 :(得分:0)

在您的代码中,

lbl1.delegate=self;

如果委托应该代表除ITSELF以外的类,那么它的值是错误的。它应该设置为其他一些委托类:

lbl1.delegate = _delegateClass;

此处:_delegateClass代表其他一些类别。

例如:(伪代码)

.h文件:

@interface ViewController1 : UIViewController

@end

.m文件:

@interface ViewController1 ()

@end

@implementation ViewController1

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

@end

.h文件:

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

.m文件:

@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

我试图尽可能多地描述/详细。

快乐编码!!