简体   繁体   English

无法从我的自定义NSObject类设置UILabel文本

[英]can't set UILabel text from my custom NSObject class

this is my viewController class where my label is connectd through IBOutlet and my ViewController.h is 这是我的viewController类,其中的标签通过IBOutlet连接,而我的ViewController.h是 在此处输入图片说明

and my ViewController.m is 而我的ViewController.m是 在此处输入图片说明

and my custom class from where I want to set ViewController label text and my custom class look like this my customVC.h look like this 我想在其中设置ViewController标签文本的自定义类和自定义类如下:customVC.h如下所示

在此处输入图片说明

在此处输入图片说明

Instead of setting the " .label " in your " init " method, do it in your " viewDidLoad: " method or your " viewWillAppear: " method. 与其在您的init方法中设置“ .label ”, .label在您的“ viewDidLoad: ”方法或“ viewWillAppear: ”方法中进行设置。

The reason for this is because at the view controller's " init " time, the label and all other user interface elements haven't been loaded from the XIB or Storyboard file yet. 这样做的原因是因为在视图控制器的“ init ”时,还没有从XIB或Storyboard文件中加载标签和所有其他用户界面元素。

ps best practice in Objective C is to start ALL variables and properties with lower case letters. ps目标C中的最佳做法是以小写字母开头所有变量和属性。 So instead of " .Label " use " .label ", or even better, something more descriptive, like " .viewControllerTitleLabel ". 因此,而不是“ .Label ”使用“ .label ”,甚至更好,更具描述性的,如“ .viewControllerTitleLabel ”。

Can I see your CustomVC.h file? 我可以看到您的CustomVC.h文件吗?

I assume that your intention is to CustomVC extend the ViewController Class. 我假设您的意图是要CustomVC扩展ViewController类。

so there's need to be 所以需要

@interface CustomVC : ViewController

in your CustomVC.h 在您的CustomVC.h中

then in your CustomVC.m it should be 然后在您的CustomVC.m中

-(id)init{
   if(self = [super init]){
       [self.Label setText:@"hello world"];
  }
  return self;
}

you don't need to allocate the viewController object anymore. 您不再需要分配viewController对象。

There is no need to alloc - init the view controller again in your customVC class, every time you alloc init something a separate reference is provided to the object, which in this case seems to be like you are setting label text for a view controller with different reference rather than the one in which you are trying to 无需分配-在您的customVC类中再次初始化视图控制器,每次您分配init时,都会为对象提供一个单独的引用,在这种情况下,这似乎就像您使用以下命令为视图控制器设置标签文本一样不同的参考,而不是您要尝试的参考

I suggest you to pass reference of your current view controller to your init methods like this 我建议您将当前视图控制器的引用传递给这样的init方法

- (id)initForVC:(ViewController *)refVC
{
    self = [super init];
    if (self) {
        [refVC.Label setText:@"Hello World"];
    }
    return self;
}

Just add this function in .m file of customVC and it's definition 只需在customVC的.m文件中添加此函数及其定义

- (id)initForVC:(ViewController *)refVC;

in .h file 在.h文件中

Your viewcontroller's viewDidLoad will change to this 您的viewcontroller的viewDidLoad将更改为此

- (void)viewDidLoad
{
    [super viewDidLoad];
    customVC *customVC = [[customVC alloc] initForVC:self];
}

instead of directly setting text to UILabel object create property of NSString class and set the text to that property and then then assign NSString property to UILabel, It will work. 而不是直接将文本设置为NSString类的UILabel对象的create属性,然后将文本设置为该属性,然后将NSString属性分配给UILabel,它将起作用。

NSString *str = [NSString stringWithFormat: @"Sample..."];
FirstViewController *f = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
f.lbl = str; //lbl is the NSString property
[self.navigationController presentViewController:f animated:YES completion:nil];

and then assign lbl to UILabel object 然后将lbl分配给UILabel对象

lblText.text = lbl;

伙计们,我通过使用NSNotificationcenter解决了这个问题

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

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