James Anderson
James Anderson

Reputation: 566

Change a UILabel's text from another class

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.

Upvotes: 0

Views: 3496

Answers (4)

Roland Keesom
Roland Keesom

Reputation: 8288

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

Upvotes: 0

Satish Azad
Satish Azad

Reputation: 2312

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"

Upvotes: 3

Paras Joshi
Paras Joshi

Reputation: 20541

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...

Upvotes: 0

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

Use delegate for messaging between classes or viewcontrollers.

Refer The Basics of Protocols and Delegates link.

Upvotes: 3

Related Questions