ramsserio
ramsserio

Reputation: 144

Edit a UILabel from another Class (another ViewController)

I want to edit a UILabel which is in ViewContrller2 from ViewController1.

This is my code, but it is not working:

ViewController1.m:

// ....

// In my viewDidLoad :

ViewController2 *vc = [[ViewController2 alloc]init];

// calling a function : 

[vc updateLabel];

// ....

ViewController2.m:

// ....

-(void)updateLabel{

    self.MyLabel.text = @"Text";

    // MyLabel is already declared in ViewController2.h

}

// ....

Please can you help me?

I've tried many codes, but it's still not working and I don't know where the problem is.

Upvotes: 1

Views: 76

Answers (4)

Rhys Lewis
Rhys Lewis

Reputation: 453

Rightly or wrongly, to achieve this I would probably use NSUserDefaults to pull info between View Controllers.

ViewController1

NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
[standardDefaults setObject:@"This is my Label" forKey:@"labelKey"];

ViewController2

NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
MyLabel.text = [standardDefaults stringForKey:@"labelKey"]

Answer updated to reflect amended question for UIProgressView as per below comment:

ViewController1.m

NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
[standardDefaults setDouble:0.75 forKey:@"ProgressValue"]; //value you want your progress view to show

ViewController2.h

... create an outlet for your progress view here and link it up in IB

  @property (weak, nonatomic) IBOutlet UIProgressView *ProgressView;

ViewController2.m

@synthesize ProgressView;

...

NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
double ProgressValue = [standardDefaults doubleForKey:@"ProgressValue"];
ProgressView.progress = ProgressValue;

Upvotes: 1

Sumeet
Sumeet

Reputation: 1055

Make sure that before calling updateLabel on ViewController2 the viewDidLoad method of ViewController2 is called otherwise self.MyLabel will be nil and hence anything you assign to self.MyLabel.text will be useless. Now to ensure that viewDidLoad of ViewController2 gets called you need to access the view property of ViewController2 since viewDidLoad of a view controller is invoked when first time the view of the controller is tried to accessed.

Once viewDidLoad the things you intend to do will work for sure. To confirm that this is indeed the problem, keep a breakpoint in the -(void)updateLabel method and analyze the to see if self.MyLabel is nil or not.

Upvotes: 0

Russell
Russell

Reputation: 5554

You are creating a new version of vc2, you need to access the one that already exists. Try sharing a reference to the vc2 controller in vc1 instead

You also need to think about whether you should update it directly from vc1 - you could refresh the label on viewDidLoad in vc2 instead

There are a number of options depending on how you have created vc1 & vc2, and there's a good description of the pros and cons here http://matteomanferdini.com/how-ios-view-controllers-communicate-with-each-other/

Upvotes: 0

Ahmed Abdallah
Ahmed Abdallah

Reputation: 2355

You're creating a new object of ViewController2 class, if you want to set this value to all views you can use:- 1-NSUserDefaults 2-SQLite 3-Core Data look to how pass data between view controllers :- Passing Data between View Controllers

Upvotes: 0

Related Questions