xcode 如何从一个视图控制器访问另一个视图控制器的变量值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2548910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I access variable values from one view controller in another?
提问by Thomas
I have an integer variable (time) in one view controller whose value I need in another view controller. Here's the code:
我在一个视图控制器中有一个整数变量 ( time),我需要在另一个视图控制器中使用它的值。这是代码:
MediaMeterViewController
媒体仪表视图控制器
// TRP - On Touch Down event, start the timer
-(IBAction) startTimer
{
time = 0;
// TRP - Start a timer
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
[timer retain]; // TRP - Retain timer so it is not accidentally deallocated
}
// TRP - Method to update the timer display
-(void)updateTimer
{
time++;
// NSLog(@"Seconds: %i ", time);
if (NUM_SECONDS == time)
[timer invalidate];
}
// TRP - On Touch Up Inside event, stop the timer, decide stress level, display results
-(IBAction) btn_MediaMeterResults
{
[timer invalidate];
NSLog(@"Seconds: %i ", time);
ResultsViewController *resultsView = [[ResultsViewController alloc] initWithNibName:@"ResultsViewController" bundle:nil];
[self.view addSubview:resultsView.view];
}
And in ResultsViewController, I want to process timebased on its value
在 ResultsViewController 中,我想根据其值处理时间
ResultsViewController
结果视图控制器
- (void)viewDidLoad
{
if(time < 3)
{// Do something}
else if ((time > 3) && (time < 6))
{// Do something else}
//etc...
[super viewDidLoad];
}
I'm kind of unclear on when @property and @synthesize is necessary. Is that the case in this situation? Any help would be greatly appreciated.
我不太清楚何时需要 @property 和 @synthesize。在这种情况下是这样吗?任何帮助将不胜感激。
Thanks! Thomas
谢谢!托马斯
回答by indragie
Declare time
as a property in MediaMeterViewController
:
在 中声明time
为属性MediaMeterViewController
:
@property (nonatomic) NSInteger time;
Whenever you need to access an instance variable in another object, you should have the instance variable declared as a property, and when you declare a property you must always use @synthesize
(to synthesize the getter and setter for that property).
每当您需要访问另一个对象中的实例变量时,您应该将该实例变量声明为一个属性,并且当您声明一个属性时,您必须始终使用@synthesize
(以合成该属性的 getter 和 setter)。
Also take note that when setting time
in MediaMeterViewController
you must always use self.time
instead of time
. For example, time = 0;
should be self.time = 0;
.
另请注意,在设置time
时MediaMeterViewController
必须始终使用self.time
而不是time
。例如,time = 0;
应该是self.time = 0;
。
To access time
from your ResultsViewController
, you would do something like this:
要从time
您的访问ResultsViewController
,您可以执行以下操作:
- (void)viewDidLoad
{
[super viewDidLoad];
if (mmvc.time < 3)
{
// Do something
}
else if ((mmvc.time > 3) && (mmvc.time < 6))
{
// Do something else
}
// etc...
}
Where mmvc
is a reference to your MediaMeterViewController
object. Hope this helps.
哪里mmvc
是对您的MediaMeterViewController
对象的引用。希望这可以帮助。