Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我必须重置ViewController中的变量吗?_Ios_Objective C - Fatal编程技术网

Ios 我必须重置ViewController中的变量吗?

Ios 我必须重置ViewController中的变量吗?,ios,objective-c,Ios,Objective C,我有ViewControllerA和ViewControllerB 在情节提要中,单击按钮后,ViewControllerA将转换到ViewControllerB 在ViewControllerB中,有另一个按钮增加值count 无论count的值是多少,当用户按下后退按钮并返回到ViewControllerB时,我假设count的值再次为0,但它仍保持在先前增加的值 为什么会这样?每次在视图中将出现或其他内容时,是否应将值重置为0 如果它很重要:变量不是属性,则在ViewControllerB

我有
ViewControllerA
ViewControllerB

在情节提要中,单击按钮后,
ViewControllerA
将转换到
ViewControllerB

ViewControllerB
中,有另一个按钮增加值
count

无论
count
的值是多少,当用户按下后退按钮并返回到
ViewControllerB
时,我假设
count
的值再次为
0
,但它仍保持在先前增加的值

为什么会这样?每次在
视图中将出现
或其他内容时,是否应将值重置为0


如果它很重要:变量不是属性,则在
ViewControllerB.m
的顶部声明为
int count=0

“如果它很重要:变量不是属性,它将在ViewControllerB.m的顶部声明为int count=0;”

是的,这很重要-您的变量与ViewControllerB没有任何关系,因此它不会被重置。 加载视图控制器时,每次加载的不是.m文件!它是其中的视图控制器


您的变量是一个全局变量,它的生命周期是应用程序的生命周期,而不是视图控制器的生命周期。

您从视图控制器a转换到视图控制器B的具体步骤是什么?我已将
ui按钮
分配到
ViewControllerB
,并使用
push
作为转换选项。
ViewController
A
B
都是同一个
NavigationController
的一部分。这很好,非常感谢。我必须等4分钟才能接受答案。快速提问:即使变量没有定义为静态变量,如果按照我的方式定义,它们对应用程序来说也是全局的?将其设置为静态会影响应用程序的哪些其他部分可以看到它,从而有效地将其设置为该文件的私有。这取决于您在何处声明了变量。在
@接口
/
@end
块或
@实现
/
@end
块之前或之内。