1

我有两个类:(DashboardViewController有一个整数变量 'userid')和LoginViewController. 我实例化 aDashboardViewController并设置useridfrom的值LoginViewController,但是当我打印 in 的值时useridDashboardViewController它会打印0。有人可以帮我吗?

这就是我实例化DashboardViewControllerLoginViewController方式 ('serverOutput' 是一个包含单个整数值的字符串:

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

然后我去DashboardViewController放:

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

这会打印 0 而不是它应该打印的整数值。任何帮助,将不胜感激。

4

1 回答 1

2

只需替换您的代码:

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

通过此代码:

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

如果您仍然没有得到答案,那么还有另一种方法......

有一种简单的方法可以解决您的问题。只需在Appdelegate类中创建一个全局变量。我的意思是,制作一个NSString属性AppDelegate.h并合成它。

AppDelegate.h:

@property(nonatomic,retain)NSString *GlobalStr;

并将其合成在AppDelegate.m.

现在制作AppDelegatein 的对象LoginViewController

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

然后去DashboardViewController放:

AppDelegate *obj = (AppDelegate*)[[UIApplication sharedApplication]delegate];
userid = [obj.GlobalStr intValue];
NSLog([NSString stringWithFormat:@"%d",userid]);
于 2013-04-23T06:15:38.227 回答