1

I know that this question is really old, but I'm having a lot of trouble accessing NSString's from another class;

Let's say that I have FirstViewController and SecondViewController, and from the Second I want to access a NSString called string - which is in FirstViewController -(okay, I know it's obvious). Here's the code of the SecondViewController:

FirstViewController *viewController = [[FirstViewController alloc] init];
NSLog(@"%@", viewController.string);

But the result is (null), because it's getting the value from the init method.

Anyone have some tip/solution to access the string from any place that it's declared?

Thanks!

4

2 回答 2

1

如果您正在做我认为您正在做的事情,那么您将FirstViewController在第二个视图控制器中创建一个新版本,除非FirstViewController.string在 init 块中将其初始化为某个值,否则它将返回一个 nul 值。

我认为您想要的是通过从已创建的项目访问项目来获取字符串的值FirstViewController。有多种方法可以做到这一点(取决于你SecondViewController从一开始是如何推动的,以及你是否使用故事板。

于 2012-08-05T23:05:57.593 回答
0

全局变量可能是您的答案。除非您在标签栏控制器中,否则不可能从另一个类中“拉取”数据。可以将您的值从 ViewController1 推送到您的 ViewController2,但为了拉取它,我想说全局变量会为您省去很多麻烦。

于 2012-08-06T04:25:38.067 回答