简体   繁体   English

IPHONE如何从其他类/视图控制器访问变量

[英]IPHONE How to access variables from other classes/viewcontrollers

I have a app that uses several view controllers. 我有一个使用多个视图控制器的应用程序。 In one instance I need to uses an int (int lives) in a seperate view controller from where it is created. 在一个实例中,我需要在创建它的单独的视图控制器中使用int(int live)。 I have tried using it and it throws and error at build claiming "lives" undeclared. 我试过使用它,它在声明未声明的“生命”时抛出错误。 I am already importing the view controller where the int was declared. 我已经在声明int的位置导入了视图控制器。 I am stuck on this one. 我被困在这一个。

I would appreciate any help. 我将不胜感激任何帮助。

使用属性,或对临时变量使用自定义的setter和getter。

You could use the application delegate to store information you need in different places in you app. 您可以使用应用程序委托将所需的信息存储在应用程序中的不同位置。

The proper way would be to implement your own delegate protocol. 正确的方法是实现自己的委托协议。 For a recent discussion on this topic, see pass a variable up the navigation stack 有关此主题的最新讨论,请参见在导航堆栈中传递变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM