Ios Cmd+click方法指向不同的ViewController
我有两个视图控制器。在这两种方法中,我都实现了-void textField didbeginediting:UITextField*textField方法。但是,当我CMD+单击第二个ViewController中的方法时,它看起来是指第一个ViewController而不是委托。参考图片 出什么事了 代码如下Ios Cmd+click方法指向不同的ViewController,ios,uiviewcontroller,uitextfielddelegate,Ios,Uiviewcontroller,Uitextfielddelegate,我有两个视图控制器。在这两种方法中,我都实现了-void textField didbeginediting:UITextField*textField方法。但是,当我CMD+单击第二个ViewController中的方法时,它看起来是指第一个ViewController而不是委托。参考图片 出什么事了 代码如下 -(void) textFieldDidBeginEditing:(UITextField *)textField { NSLog(@"in text edit %@ ",text
-(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];
在代码中
lbl1.代表=自我
若委托应该表示类而不是它本身,那个么它的值是错误的。应将其设置为其他某个委托类:
lbl1.delegate=_delegateClass
这里:_delegateClass表示除自身之外的其他类
例如:psuedocode
.h文件:
@interface ViewController1 : UIViewController
@end
@interface ViewController2 : UIViewController
{
// declare textfield here
}
@property id _delegate;
@end
.m文件:
@interface ViewController1 ()
@end
@implementation ViewController1
- (void)viewDidLoad
{
// initialize view controller here
// set _delegate to self
}
@end
@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
.h文件:
@interface ViewController1 : UIViewController
@end
@interface ViewController2 : UIViewController
{
// declare textfield here
}
@property id _delegate;
@end
.m文件:
@interface ViewController1 ()
@end
@implementation ViewController1
- (void)viewDidLoad
{
// initialize view controller here
// set _delegate to self
}
@end
@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
我现在尽可能多地描述/详细
快乐编码 我猜在一个控制器中,你做了cmd+点击,所以它指的是剩下的一个。是的。。。。但问题是。。。为什么?此功能选择此签名的可用位置,单击后跳转到相应的类。因此,没有必要跳到同一个类上,因为您已经找到了它。实际上,我并不是在谈论这个特性。发生的事情是,在执行该委托之后,另一个类的viewDidLoad方法将被执行。然后检查委托的值,它在运行时引用了正确的类吗?您演示的方式无助于找到执行时类出错的原因。使用NSLogto打印或放置断点和调试窗口try:po delegate或print delegate在运行时查看实际的类绑定。