简体   繁体   English

在Viewcontroller中创建文本字段并在NsObject类中退出文本字段

[英]Creating textfield in Viewcontroller and resigning textfield in NsObject class

I have to resign textfield that has created in a view controller, and need to resign that textfield in an NSObject class. 我必须辞职在视图控制器中创建的文本字段,并且需要辞职在NSObject类中的该文本字段。 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. 如果我在NSObject中为视图控制器创建一个对象,它将创建一个新实例,并且无法访问该文本字段。 My code is 我的代码是

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类中辞职文本字段。

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. 因此,尝试以不应该在timerFired方法中timerFired初始化视图控制器的方式修改代码。 But instead you can directly check the instance of your view controller and if exist then try to access the textfield. 但是,您可以直接检查视图控制器的实例,如果存在,则尝试访问文本字段。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM