如何从另一个视图控制器访问变量值? [英] How do I access variable values from one view controller in another?

查看:66
本文介绍了如何从另一个视图控制器访问变量值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个视图控制器中有一个整数变量(时间),我在另一个视图控制器中需要它的值。这是代码:

I have an integer variable (time) in one view controller whose value I need in another view controller. Here's the code:

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];
}

在ResultsViewController中,我想处理时间基于其价值

And in ResultsViewController, I want to process time based on its value

ResultsViewController

- (void)viewDidLoad 
{
    if(time < 3)
       {// Do something}

    else if ((time > 3) && (time < 6))
       {// Do something else}

//etc...

    [super viewDidLoad];
}

我有点不清楚何时需要@property和@synthesize。在这种情况下是这样的吗?任何帮助将不胜感激。

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.

谢谢!
Thomas

Thanks! Thomas

推荐答案

声明时间作为 MediaMeterViewController

@property (nonatomic) NSInteger time;

每当您需要访问另一个对象中的实例变量时,您应该将实例变量声明为属性,当你声明一个属性时,你必须始终使用 @synthesize (合成该属性的getter和setter)。

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).

另请注意,在 MediaMeterViewController 中设置时间时,必须始终使用 self .time 而不是时间。例如, time = 0; 应为 self.time = 0;

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;.

要从 ResultsViewController 访问时间,你会做这样的事情:

To access time from your ResultsViewController, you would do something like this:

- (void)viewDidLoad 
{
    [super viewDidLoad];
    if (mmvc.time < 3)
    {
        // Do something
     }

    else if ((mmvc.time > 3) && (mmvc.time < 6))
    {
    // Do something else
    }

    // etc...    
}

其中 mmvc 是对你的引用 MediaMeterViewController 对象。希望这会有所帮助。

Where mmvc is a reference to your MediaMeterViewController object. Hope this helps.

这篇关于如何从另一个视图控制器访问变量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆