简体   繁体   English

从另一个类访问类变量

[英]Access class variable from another class

I have a UITabBarController that manages two ViewControllers. 我有一个UITabBarController,它管理两个ViewController。 The first is a UIViewController that allows the user to change game settings. 第一个是允许用户更改游戏设置的UIViewController。 The second is a GLKViewController that runs the game simulation. 第二个是运行游戏模拟的GLKViewController。

I'm trying to enable the Game ViewController to fetch the settings from the Settings ViewController. 我试图使Game ViewController能够从Settings ViewController中获取设置。 I have a Slider on the Settings View that represents "Speed". 我在“设置”视图上有一个表示“速度”的滑块。

I have a reference to the other controller, but I'm unable to expose the variable that backs my Slider properly. 我有对另一个控制器的引用,但是我无法公开正确支持我的Slider的变量。

SecondViewController.h SecondViewController.h

@interface SecondViewController : UIViewController{
    IBOutlet UISlider    * mySlider;
}
property (nonatomic,retain) IBOutlet UISlider    * mySlider;
@end

SecondViewController.m SecondViewController.m

- (IBAction) mySliderWasMoved:(id)sender;
@implementation SecondViewController
@synthesize mySlider;
- (IBAction) mySliderWasMoved:(id)sender{    
};

ThirdViewController.m ThirdViewController.m

NSArray *tmpVCs = self.parentViewController.tabBarController.viewControllers;
UIViewController *tmpVC = [tmpVCs objectAtIndex:1]; //obtain handle to SecondViewController
//NSNumber *mySpeed = tmpVC.mySlider;  //doesn't see mySlider
NSNumber *mySpeed = [tmpVC mySlider];  //doesn't see mySlider

I'm new to this, and there are many aspects of my project to learn - so I'm not trying to learn how to manage data at this time. 我对此并不陌生,我的项目有很多方面需要学习-所以我现在不尝试学习如何管理数据。 I just need to know how to access an instance variable 我只需要知道如何访问实例变量

As mention on the comments, 正如评论中提到的那样,

  1. Use NSDefault to save the value on slider changed. 使用NSDefault将值保存在已更改的滑块上。 On the very first time of loading your application, you will want to set a default value. 在第一次加载应用程序时,您将需要设置一个默认值。

  2. Use Singleton Object to store value. 使用Singleton对象存储值。

We understand that, quoting from you " not trying to learn data persistence at this time. Nor do I need architecture direction.", but the rule of thumb here is that you probably will be able to access the instance variable in some way or the other but i think having the best approach will benefit you greatly. 我们了解到,引用“您现在不尝试学习数据持久性。我也不需要体系结构指导。”,但是这里的经验法则是,您可能将能够以某种方式或方法访问实例变量。其他,但我认为拥有最好的方法将使您受益匪浅。

Just my 2 cent. 只是我的2美分。

Fort the benefit of others: I grabbed a handle to the other class, but I hadn't declared the return type as the correct type of class. 让其他人受益:我抓住了另一个类的句柄,但没有将返回类型声明为正确的类类型。

Replace: 更换:

UIViewController *tmpVC = [tmpVCs objectAtIndex:1]; 

With: 带有:

SecondViewController *tmpVC = [tmpVCs objectAtIndex:1]; 

Now I have access to the properties that are specific to the SecondViewController. 现在,我可以访问特定于SecondViewController的属性。

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

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