【发布时间】:2013-04-16 03:04:03
【问题描述】:
我有两个类:DashboardViewController(有一个整数变量'userid')和LoginViewController。我实例化一个DashboardViewController 并从LoginViewController 设置userid 的值,但是当我在DashboardViewController 中打印userid 的值时,它会打印0。有人可以帮我解决这个问题吗?
这就是我从LoginViewController 实例化DashboardViewController 的方式('serverOutput' 是一个包含单个整数值的字符串:
DashboardViewController *newView = [[DashboardViewController alloc] init];
[self presentViewController:newView animated:YES completion:nil];
newView.userid = [serverOutput intValue];
然后我转到DashboardViewController 并输入:
NSLog([NSString stringWithFormat:@"%d",userid]);
这会打印 0 而不是它应该打印的整数值。任何帮助将不胜感激。
【问题讨论】:
-
您确定
[serverOutput intValue]正在返回一个非0 值吗?记录[serverOutput intValue]。注意字符串中的意外空格。 -
注意:你应该在调用
presentViewController之前设置newView.userId。此外,您的NSLog应该是:NSLog(@"%d", userid);。NSLog已经处理字符串格式。 -
@rmaddy - 谢谢,我是 XCode 的新手,您的建议很有帮助!是的 [serverOutput intValue] 返回一个非 0 值,我不知道是不是因为它只有一个数字。您知道将字符串“serverOutput”转换为整数的更好方法吗?
-
您现在知道您对
newView.userId的调用被设置为非零值。现在您需要确定为什么它记录为零。您的userId的日志在DashboardViewController的哪个位置?确保在LoginViewController中设置它后调用它。此外,将日志更改为打印出self.userId而不仅仅是userId。看看有没有帮助。 -
@rmaddy- 是的,我现在得到了预期的输出。谢谢你的帮助! :)
标签: objective-c uiviewcontroller nsstring integer