1

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.

Brian
  • 14,610
  • 7
  • 35
  • 43
ramsserio
  • 144
  • 1
  • 2
  • 11

4 Answers4

1

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;
Rhys Lewis
  • 453
  • 4
  • 20
  • the problem is that this situation is just an example In fact I need that to control a uiprogressview from another VC – ramsserio Feb 01 '16 at 19:52
  • That's a different question really but my approach is still valid. Use NSUserDefaults to pass the value between the controllers, and leave your progressview with the controller to which it belongs, but use your NSUserDefaults value to update it. This way, a value changed in one controller can be passed to your progressview residing in another controller. – Rhys Lewis Feb 01 '16 at 20:01
  • I have amended my answer. Please mark as answered if it has answered your question. Best regards. – Rhys Lewis Feb 01 '16 at 20:44
0

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/

Russell
  • 5,436
  • 2
  • 19
  • 27
0

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

Community
  • 1
  • 1
Ahmed Abdallah
  • 2,338
  • 1
  • 19
  • 30
0

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.

Sumeet
  • 1,055
  • 8
  • 18
  • i ve tried to call `[vc viewDidLoad];` before calling `updateLabel` but it's still not working – ramsserio Feb 01 '16 at 19:55
  • you should not call `viewDidLoad` explicitly. Try accessing `vc.view`. For now jus print the frame of vc.view and then call `updateLabel` – Sumeet Feb 01 '16 at 20:03
  • Also did you try keeping the breakpoint? Does sit show `self.MyLabel` to be nil? – Sumeet Feb 01 '16 at 20:06