我有一个Temp类,它存储了很多UITextField。我使用另一个类EditViewController来显示这些UITextField,我想在EditViewController类中调用UITextField委托。那我怎么能实现这个呢?
我已经尝试过以下代码而没有运气。
ViewController *viewConnection = [self.storyboard instantiateViewControllerWithIdentifier:@"EditViewController"];
tfQue = [[UITextField alloc] initWithFrame:CGRectMake(5, 10, viewLoc.frame.size.width - 90, 25)];
tfQue.placeholder = @"tap here to type description for date";
tfQue.layer.borderWidth = 0;
tfQue.textColor = [UIColor blueColor];
tfQue.tag = tag;
tfQue.delegate = viewConnection;
上面的代码是在Temp类的函数中编写的。下面是在EditViewController中访问此函数的代码:
newElement = [tempObj createTextfieldWithQuestion:strQuestionText withOriginY:posY withIndexTag:i + 1];
[self.view addSubview:newElement];
答案 0 :(得分:1)
传递自引用将其指定为TextField Delegate。
- (UIView *)createTextfieldWithQuestion:(NSString *)strQuestionText withOriginY:(NSInteger)posY withIndexTag:(NSInteger)indexTag setDelegateForUITextField:(id)delegateParent {
tfQue = [[UITextField alloc] initWithFrame:CGRectMake(5, 10, viewLoc.frame.size.width - 90, 25)];
tfQue.placeholder = @"tap here to type description for date";
tfQue.layer.borderWidth = 0;
tfQue.textColor = [UIColor blueColor];
tfQue.tag = tag;
tfQue.delegate = delegateParent;
}
致电
[tempObj createTextfieldWithQuestion:strQuestionText withOriginY:posY withIndexTag:i + 1 setDelegateForUITextField:self];
答案 1 :(得分:0)
我已经解决了这个问题。以下是解决方案。
-(UIView*)createTextfieldWithQuestion: (NSString *)text withOriginY: (CGFloat)posY withIndexTag: (int)tag withTarget:(ViewController *)vc
{
UIView *viewLoc = [[UIView alloc] initWithFrame:CGRectMake(5, posY, SCREEN_WIDTH - 10, TF_VIEW_HEIGHT)];
viewLoc.backgroundColor = [UIColor whiteColor];
tfQue = [[UITextField alloc] initWithFrame:CGRectMake(5, 10, viewLoc.frame.size.width - 90, 25)];
tfQue.placeholder = @"tap here to type description for date";
tfQue.layer.borderWidth = 0;
tfQue.textColor = [UIColor blueColor];
tfQue.tag = tag;
tfQue.delegate = vc;
...
...
[viewLoc addSubview:tfQue];
return viewLoc;
}
上面的代码是在Temp类的函数中编写的。下面是在EditViewController中访问此函数的代码:
newObj = [tFunc createTextfieldWithQuestion:strQuestionText withOriginY:posY withIndexTag:i + 1 withTarget:self];
[self.view addSubview:newElement];