从另一个类更改UILabel的文本 [英] Change a UILabel's text from another class

查看:61
本文介绍了从另一个类更改UILabel的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图控制器和一个类(我正在使用情节提要).如何从类中更改UILabel的文本(在视图控制器中)?我已经尝试过了,但是无济于事:

I have a view controller and a class (I am using storyboards). How can I change the text of a UILabel (in the view controller) from the class? I have tried this, but to no avail:

ViewController *mainView = [[ViewController alloc] init];
[mainView view];

[mainView.progressBar setProgress:integer animated:YES];

NSLog(@"Updated Progress Bar");

NSString *progressLabelText = [NSString stringWithFormat:@"%@ out of %i followers", userString, [self.followers count]];

[mainView.progressLabel setText:progressLabelText];

NSLog(@"Updated Progress Label Text: %@", progressLabelText);

使用此代码,文本不会更改.我应该怎么做呢?

The text does not change using this code. What should I be doing instead?

ViewController的.h文件中的进度条和标签如下:

The progressbar and label in the ViewController's .h file look like this:

@property (nonatomic, strong) IBOutlet UIProgressView *progressBar;
@property (nonatomic, strong) IBOutlet UILabel *progressLabel;

它们在Interface Builder中已完全链接起来.

And they are fully linked up in Interface Builder.

推荐答案

尝试使用Notification来更新标签文本.

Try to use Notification for updating the Lable text.

在您的viewController中编写以下代码:

in your viewController write this:

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLabel) name:@"lblUpdate" object:nil];


  and selector is :
   -(void)updateLabel
{
    self.lblObject.text = @"Your updated text"
}

,现在在您的班级中,使用发布通知来调用它:

and now in your Class call this using Post notification:

 [[NSNotificationCenter defaultCenter] postNotificationName:@"lblUpdate" object:nil];

记住使用相同的通知名称"lblUpdate"

remember use same notification name "lblUpdate"

这篇关于从另一个类更改UILabel的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆