1

I have two classes: DashboardViewController (having an integer variable 'userid') and LoginViewController. I instantiate a DashboardViewController and set the value of userid from LoginViewController, but when I print the value of userid in DashboardViewController, it prints 0. Could someone please help me with this?

This is how I instantiate DashboardViewController from LoginViewController ('serverOutput' is a string containing a single integer value:

DashboardViewController *newView = [[DashboardViewController alloc] init];
[self presentViewController:newView animated:YES completion:nil];
newView.userid = [serverOutput intValue];

I then go to DashboardViewController and put:

NSLog([NSString stringWithFormat:@"%d",userid]);

and this prints 0 instead of the integer value it should. Any help would be appreciated.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
spatra
  • 151
  • 1
  • 3
  • 10
  • 3
    Are you sure that `[serverOutput intValue]` is returning a non-0 value? Log `[serverOutput intValue]`. Keep an eye out for unexpected whitespace in the string. – rmaddy Apr 23 '13 at 03:11
  • 2
    Notes: You should set `newView.userId` before you call `presentViewController`. Also, your `NSLog` should be: `NSLog(@"%d", userid);`. `NSLog` already handles string formats. – rmaddy Apr 23 '13 at 03:12
  • @rmaddy - Thanks, I am new to XCode and your suggestions helped a lot! And yes [serverOutput intValue] returns a non-0 value, I don't know if its because it is only a single digit. Do you know a better way of converting the string 'serverOutput' into an integer? – spatra Apr 23 '13 at 03:48
  • You now know that your call to `newView.userId` is being set to a non-zero value. Now you need to determine why it is logging as zero. Where in `DashboardViewController` is your log of `userId`? Make sure it is being called after you set it in your `LoginViewController`. Also, change the log to print out `self.userId` instead of just `userId`. See if that helps. – rmaddy Apr 23 '13 at 03:52
  • @rmaddy- yeah I get the expected output now. Thanks for your help! :) – spatra Apr 23 '13 at 04:04
  • Duplicate of http://stackoverflow.com/questions/5488135/objective-c-how-to-access-an-instance-variable-from-another-class – Arpit Kulsreshtha Apr 25 '13 at 09:20

1 Answers1

2

just replace your code :

DashboardViewController *newView = [[DashboardViewController alloc] init];
[self presentViewController:newView animated:YES completion:nil];
newView.userid = [serverOutput intValue];

By this code :

DashboardViewController *newView = [[DashboardViewController alloc] init];
newView.userid = [serverOutput intValue];
[self presentViewController:newView animated:YES completion:nil];

And if you still not get your answer,Then there is an alternate method too...

There is an easy approach to solve your problem. Just make a global variable in Appdelegate class. I mean ,make a NSString property in AppDelegate.h and synthesize it.

In AppDelegate.h:

@property(nonatomic,retain)NSString *GlobalStr;

And synthesize it in AppDelegate.m.

Now make Object of AppDelegate in LoginViewController :

AppDelegate *obj = (AppDelegate*)[[UIApplication sharedApplication]delegate];
DashboardViewController *newView = [[DashboardViewController alloc] init];
obj.GlobalStr = [serverOutput intValue];
[self presentViewController:newView animated:YES completion:nil];

Then go to DashboardViewController and put:

AppDelegate *obj = (AppDelegate*)[[UIApplication sharedApplication]delegate];
userid = [obj.GlobalStr intValue];
NSLog([NSString stringWithFormat:@"%d",userid]);
Vineet Singh
  • 4,009
  • 1
  • 28
  • 39