0

I have a UserDetails class. I have a function in another class B(TableViewController) which contains an object of the UserDetails class, something like this:

    -(void)sendData: (UserDetails *) user {

            //some code
      }

Now I have another function in a third class C (TableViewController) where I want to use the value contained in the variable "user". How do I do it?

3 Answers3

0

In the third class, set up a property of type UserDetails.

When you move from B to C (either in the prepareForSegue or where you init the TVC and push it) just pass the UserDetails object into C so it has its own version. That way you keep is simple.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • can you tell me the commands..coz I'm getting this message on the console:Unbalanced calls to begin/end appreance transitions for MainViewController . (MainViewController is the parent View Controller which I've created) . .In the sendData function : -(void)sendData:(UserDetails*) user{ ClassCTableViewController *controller1 = [[ClassCTableViewController alloc] initWithNibName:@"ClassCTableViewController" bundle:nil]; controller.userObj =user; [self.navigationController pushViewController:controller1 animated:YES]; [controller1 release]; } – HakunaMatata Sep 24 '12 at 09:00
0

In your third class C .h file create a UserDetails object

@property (nonatomic,strong) UserDetails * userObj;

Then in your class just before where your present/push your third class, lets say thirdClassObj, do this

thirdClassObj.userObj=user;
Neo
  • 2,807
  • 1
  • 16
  • 18
  • I was already doing it...In the sendData function : -(void)sendData:(UserDetails*) user{ ClassCTableViewController *controller = [[ClassCTableViewController alloc] initWithNibName:@"ClassCTableViewControlle" bundle:nil]; controller.userObj =user; [self.navigationController pushViewController:controller1 animated:YES]; [controller1 release]; } I am not getting any error but some message is getting printed on the console: Unbalanced calls to begin/end appreance transitions for MainViewController . (MainViewController is the parent View Controller which I've created) – HakunaMatata Sep 24 '12 at 08:46
  • why are you pushing view to controller1 or is that suppose to be controller? even you nib name is wrong – Neo Sep 24 '12 at 08:48
  • I dont want to push the view to controlelr1. but if I'm skipping that statement I am getting an exception. – HakunaMatata Sep 24 '12 at 08:51
  • nib name is just a typo here. :) – HakunaMatata Sep 24 '12 at 08:52
0

I remember answering a similar question. Use of singleton will be the best approach according to me. You can also see my answer regarding use of singleton for global variables.
You might also use appDelegate for this purpose. But I won't recommend using appDelegate. See this for clarification.

Community
  • 1
  • 1
Nitish
  • 13,845
  • 28
  • 135
  • 263
  • that variable cannot be made global for all the views because its value changes depending on my service URL's. – HakunaMatata Sep 24 '12 at 08:54
  • @HakunaMatata: Ok. In that case you can save the value in a variable by following the above approach and use it when you need to. – Nitish Sep 24 '12 at 09:11