0

I have a UITabBarController that manages two ViewControllers. The first is a UIViewController that allows the user to change game settings. The second is a GLKViewController that runs the game simulation.

I'm trying to enable the Game ViewController to fetch the settings from the 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.

SecondViewController.h

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

SecondViewController.m

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

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

torpedo51
  • 182
  • 2
  • 13
  • Try this http://stackoverflow.com/questions/7058858/should-i-use-nsuserdefaults-or-a-plist-to-store-data – GTSouza Jul 13 '12 at 01:53
  • Agreed, you don't use view controllers to hold data like settings. Something like settings probably belongs in some persistent storage (i.e. you probably want settings to persist, even after the user quits). GTSouza's link reference two nice and simple forms of persistent storage. Thus, rather than the game retrieving the settings from the settings view controller (whose only purpose is only to read, change, and then update the persistent storage), it would get it from the persistent storage directly. – Rob Jul 13 '12 at 03:00
  • As I said, I'm not trying to learn data persistence at this time. Nor do I need architecture direction. This is a sample project and my question is why I can't access a variable on another class that I appear to have exposed properly. Thanks in advance. – torpedo51 Jul 13 '12 at 05:18

2 Answers2

0

As mention on the comments,

  1. Use NSDefault to save the value on slider changed. On the very first time of loading your application, you will want to set a default value.

  2. Use Singleton Object to store value.

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.

lancegoh
  • 624
  • 1
  • 6
  • 16
  • The UISlider already has a default value. I'd like to set the backing variable/property from the slider, not the other way around. Every time the slider changes, that should set the property, and then the game will fetch the new value. I don't need persistence for this functionality. – torpedo51 Jul 19 '12 at 19:04
0

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.

torpedo51
  • 182
  • 2
  • 13