Change a UILabel's text from another class

血红的双手。 提交于 2019-12-23 05:04:37

问题


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?

Edit: 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;

And they are fully linked up in Interface Builder.


回答1:


Use delegate for messaging between classes or viewcontrollers.

Refer The Basics of Protocols and Delegates link.




回答2:


Try to use Notification for updating the Lable text.

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];

remember use same notification name "lblUpdate"




回答3:


just property synthesize the string and then set that string in viewWillAppear: method of ViewController.m class

just take NSString variable in ViewController.h file like bellow..

@property (nonatomic, retain) NSString *progressText;

and synthesize this in ViewController.m file like bellow..

@synthesize progressText;

after that in viewDidLoad: method just set text at starting to the progressLableText like bellow...

- (void)viewDidLoad
{

     progressText = @"Your Text";
     [progressLabel setText:progressText];

}

and also in viewWillAppear: set the text like above...

- (void)viewWillAppear:(BOOL)animated
{
    [progressLabel setText:progressText];
}

and in your code just change something like bellow..

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.progressText = progressLabelText;
[mainView.progressText retain];


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

i hope this helpful to you...




回答4:


But use a delegate to do the callbacks. In your spamchecker.h add:

@protocol SpamCheckerDelegate <NSObject>
-(void)updateText:(NSString*)newText;
@end

@property (nonatomic, weak) id <SpamCheckerDelegate> delegate;

In your spamchecker.m where you need it add:

[self.delegate updateText:@"newText"];

Then in your mainView.h add:

@interface MainView : UIViewController <SpamCheckerDelegate>

In your mainview.m add:

spamchecker.delegate = self;

And implement the method like:

-(void)updateText:(NSString*)newText
{
    [self.progressLabel setText:progressLabelText];
}


来源:https://stackoverflow.com/questions/14196708/change-a-uilabels-text-from-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!