Ios 如何在不同的Objective-C类中调用UITextField委托方法?
我有一个类Temp,它存储了很多UITextField。我使用另一个类EditViewController来显示这些UITextField,我想在EditViewController类中调用UITextField委托。那么我如何才能做到这一点呢 我已经尝试了下面的代码,但没有成功Ios 如何在不同的Objective-C类中调用UITextField委托方法?,ios,objective-c,uitextfield,uitextfielddelegate,Ios,Objective C,Uitextfield,Uitextfielddelegate,我有一个类Temp,它存储了很多UITextField。我使用另一个类EditViewController来显示这些UITextField,我想在EditViewController类中调用UITextField委托。那么我如何才能做到这一点呢 我已经尝试了下面的代码,但没有成功 ViewController *viewConnection = [self.storyboard instantiateViewControllerWithIdentifier:@"EditViewControlle
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];
newObj = [tFunc createTextfieldWithQuestion:strQuestionText withOriginY:posY withIndexTag:i + 1 withTarget:self];
[self.view addSubview:newElement];
传递自引用以将其指定为TextField委托
- (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;
}
召唤
我已经解决了这个问题。下面是解决方案
-(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中访问此功能的代码:
newElement = [tempObj createTextfieldWithQuestion:strQuestionText withOriginY:posY withIndexTag:i + 1];
[self.view addSubview:newElement];
newObj = [tFunc createTextfieldWithQuestion:strQuestionText withOriginY:posY withIndexTag:i + 1 withTarget:self];
[self.view addSubview:newElement];
“我有一个类Temp,它存储了很多UITextField。我使用另一个类EditViewController来显示这些UITextField”-为什么?在视图/视图控制器之外存储UI元素的目的是什么?我正在开发一个允许用户创建不同表单/模板的应用程序。因此,我有一个公共类,其中存储了Xcode提供的大多数UI元素。如何在EditViewController中访问/显示tfQue textfield。?@HirenPrajapati那么使用factory类可能是有意义的,但存储实例是非常荒谬的。@luckyShubhra在EditVC中,我调用函数来创建驻留在Temp类中的UI。我使用instant of Temp类并调用返回UIView并存储在EditVC视图中的函数。请参阅我的最新问题。