0

I have two controllers A & B In B there are one label and one string In A i have write the below code

B *ObjB =[[B alloc]initWithNibName:@"B" bundle:nil];
ObjB.Mylabel.text=[NSString stringWithString:@"Add New name"];
ObjB.MyString=[NSString stringWithString:@"new string name"];
[self.navigationController pushViewController:ObjB animated:YES];
[ObjB release]; 

I am only getting the ObjB.MyString value in B , not getting the label text.Can any one help.Thanks in advance.

Melbourne
  • 531
  • 3
  • 24
  • 2
    Have you tried setting the line `ObjB.Mylabel.text=[NSString stringWithString:@"Add New name"];` after the line `[self.navigationController pushViewController:ObjB animated:YES];`? – Ananth Aug 01 '12 at 08:45
  • It depends on your need. you can make a Singleton class for Sharing data between classes. you can see my previous answer how to make Singleton class [here](http://stackoverflow.com/questions/11272864/ios-passing-variable-to-view-controller/11272989#11272989) – QueueOverFlow Aug 01 '12 at 08:51

3 Answers3

1

Well assuming that MyLabel is a UILabel (sidenote: avoid capitalized names for ivars - in Objc capitalized names are used [by convention] for Classes), the reason that the value is not set is because the view hierarchy of your B controller has not yet been loaded (ie your label is nil at this point). So you got three options:

  1. Force the view hierarchy to load & then set your label:

    [ObjB loadView]; // Note: Apple says that you never should call this directly (just to illustrate my point)!!

  2. Let the system load the hierarchy for you by requesting the view first:

    id view = ObjB.view; // This is a bit of a 'hack' actually

  3. Just add another property in your B controller, set that and on viewDidLoad set your label's text (This is the best option in my opinion)

Community
  • 1
  • 1
Alladinian
  • 34,483
  • 6
  • 89
  • 91
  • I wouldn't force load the view hierarchy. The view are lazy loaded for a reason, let the system decide when to load it. The second option of storing the data and populating the label when the system decides to load the view is much better. – Stew Aug 01 '12 at 08:56
  • @Stew Agree. And that's why I have the exclamation marks in my comments :) – Alladinian Aug 01 '12 at 08:57
  • Sorry, missed that the first time. I'd only offer option #3, the others are both solutions I wouldn't recommend – Stew Aug 01 '12 at 09:05
1

The best way to pass data between two view controllers is to declare a variable in controller B, to hold the labels text. In viewController B's header file

    NSString *labelText;

//Declare its property and synthesize in .m

In controller A, before navigating to controller B, initialize this variable to the text you want, i.e. in this case "Add New Name".

B *ObjB =[[B alloc]initWithNibName:@"B" bundle:nil];
ObjB.labelText = [NSString stringWithString:@"Add New name"];
ObjB.MyString = [NSString stringWithString:@"new string name"];
[self.navigationController pushViewController:ObjB animated:YES];

Next in the viewDidLoad of controller B, assign the label's text to the variable which contains the string.

ViewDidLoad of B
MyLabel.Text = labelText;
//Assuming you have mapped MyLabel to the IB. 

Also i use ARC for all my projects, so I dont use the release command.

footyapps27
  • 3,982
  • 2
  • 25
  • 42
0

This solved my problem

B *ObjB =[[B alloc]initWithNibName:@"B" bundle:nil];
[self.navigationController pushViewController:ObjB animated:YES];
ObjB.Mylabel.text=[NSString stringWithString:@"Add New name"];
ObjB.MyString=[NSString stringWithString:@"new string name"];
[ObjB release]; 

Thanks for your immediate response

Melbourne
  • 531
  • 3
  • 24
  • Please follow @Alladinian's solution. I'm not sure your approach will consistently result in the view hierarchy being created before you set the property on the label. – Stew Aug 01 '12 at 09:17