在Viewcontroller中创建文本字段并在NsObject类中重新签名文本字段

时间:2022-09-24 22:51:39

I have to resign textfield that has created in a view controller, and need to resign that textfield in an NSObject class. If I create an object for the view controller in NSObject it will create an new instance and can’t able to access that textfield. My code is

我必须重新签名在视图控制器中创建的文本字段,并且需要在NSObject类中重新签名该文本字段。如果我在NSObject中为视图控制器创建一个对象,它将创建一个新实例,并且无法访问该文本字段。我的代码是

ViewController.m

ViewController.m

    -(void)viewWillAppear:(BOOL)animated
    {
         cardNo1=[self createTextField:80  :230 :45 :26 :@"Helvetica Light":NSTextAlignmentCenter];

         cardNo2=[self createTextField:128 :230 :45 :26 :@"Helvetica Light":NSTextAlignmentCenter];
         cardNo3=[self createTextField:176 :230 :45 :26 :@"Helvetica Light":NSTextAlignmentCenter];
         cardNo4=[self createTextField:224 :230 :45 :26 :@"Helvetica Light":NSTextAlignmentCenter];
         cardNo5=[self createTextField:272 :230 :35 :26 :@"Helvetica Light":NSTextAlignmentCenter];

    }

NSObject.m

NSObject.m

-(void)_timerFired
{
    ViewContoller *iphoneobj=[[ViewContoller alloc]init];
    [iphoneobj1.cardNo1 resignFirstResponder];
    [iphoneobj1.cardNo2 resignFirstResponder];
    [iphoneobj1.cardNo3 resignFirstResponder];
    [iphoneobj1.cardNo4 resignFirstResponder];

}

In NSObject class it will create an new instance for view controller, so cardNo1, cardNo2, etc will return nil object, Can any one suggest how to resign text field in NSObject class

在NSObject类中它将为视图控制器创建一个新实例,因此cardNo1,cardNo2等将返回nil对象,任何人都可以建议如何在NSObject类中重新签名文本字段

2 个解决方案

#1


0  

You cannot do like this, if you are allocating your view controller then inside that you need to initialise your textfield also after that only you can access it. So try to modify your code in such a way that you should not alloc init your view controller inside your timerFired method. But instead you can directly check the instance of your view controller and if exist then try to access the textfield.

你不能这样做,如果你正在分配视图控制器,那么你需要初始化你的文本字段,之后只有你可以访问它。因此,尝试修改代码的方式是不要在timerFired方法中分配init视图控制器。但您可以直接检查视图控制器的实例,如果存在则尝试访问文本字段。

#2


0  

I got my answer by creating an object of that view controller in NSObject class and the value of that object will be assigned in view controller class and i will access my textfield using that object in NsObject class.

我通过在NSObject类中创建该视图控制器的对象得到了我的答案,该对象的值将在视图控制器类中分配,我将使用NsObject类中的该对象访问我的文本字段。

#1


0  

You cannot do like this, if you are allocating your view controller then inside that you need to initialise your textfield also after that only you can access it. So try to modify your code in such a way that you should not alloc init your view controller inside your timerFired method. But instead you can directly check the instance of your view controller and if exist then try to access the textfield.

你不能这样做,如果你正在分配视图控制器,那么你需要初始化你的文本字段,之后只有你可以访问它。因此,尝试修改代码的方式是不要在timerFired方法中分配init视图控制器。但您可以直接检查视图控制器的实例,如果存在则尝试访问文本字段。

#2


0  

I got my answer by creating an object of that view controller in NSObject class and the value of that object will be assigned in view controller class and i will access my textfield using that object in NsObject class.

我通过在NSObject类中创建该视图控制器的对象得到了我的答案,该对象的值将在视图控制器类中分配,我将使用NsObject类中的该对象访问我的文本字段。