Very basic objective-c question-how do I make different views share information?
So, basically, I have four different views that are switched via a tab bar at the bottom开发者_如何学JAVA of the screen. I have a view controller set up for each one, and they all work fine. The only problem is that I have no idea how to share information between the different views-for example, I have an IBAction on one that takes the output of a UISegmentedControl and stores the selection as an integer (1,2,3,4). The problem is, I don't know how to access that integer from the first view controller (the one that actually does stuff with that information). I'm sure this is a very basic problem but my google-fu doesn't seem to be working here. Thanks!
Using the MVC method, information should not be stored in the view controllers. You should have a separate object which stores the information, the view controllers load it, and the views display it. The easiest way to do this would be to store the information in your application's delegate. Then, whenever the view controller needs to find/change some information, it can use [[UIApplication sharedApplication] delegate]
to get the delegate, and load/change the information as needed. Load the information to update the display in viewDidLoad or viewWillDisplay:, and change it in action methods.
Edit Example:
DelegateClass.h
@interface DelegateClass : NSObject {
//ivars
float number1;
}
@property (assign) float number1;
...
@end
DelegateClass.m
#import "DelegateClass.h"
@implementation DelegateClass
@synthesize number1;
...
@end
MyViewController.m
#import "DelegateClass.h"
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
DelegateClass *delegate = [[UIApplication sharedApplication] delegate];
float number1 = delegate.number1;
...
}
- (IBAction)actionMethod:(id)sender {
float number1 = sender.floatValue;//get new value
((DelegateClass*)[[UIApplication sharedApplication] delegate]).number1 = number1;
}
...
@end
You could have your views expose their variables to the UITabBarController
. The UITabBarController
can have methods that these views can call to see these variables.
Or, you could have your views directly set the values of variables in the UITabBarController
. Not recommended (though a lot less typing :)
Just did a quick search on Google, btw, and came across this: Accessing parent view controller (custom) properties. Looks like there's sample code to be had there. :)
Hope this helps!
I use a general theme in all my apps where I have certain data that needs to be accessed practically in all main classes. I find it really easy to use and practically error free. (Thanks again to all the folks here in the forum for the super idea!)
I declare this variable in all the classes where I need it...
@class WebiDataManager;
@interface FirstViewController : UIViewController<MFMessageComposeViewControllerDelegate, SetupDelegate> {
WebiDataManager *webiDataManager;
...
In this code I get it from the AppDelegate...
#import "WebiAppDelegate.h"
- (void)viewWillAppear:(BOOL)animated {
// D_IN;
[super viewWillAppear:animated];
//get the dataManager global Object, so we always have a structured accesss to the data!
WebiAppDelegate *mainDelegate = (WebiAppDelegate *)[[UIApplication sharedApplication]delegate];
self.webiDataManager = mainDelegate.webiDataManager;
By the way, this also works great for coredata, since through this global variable I always have access to all the main coredata.
Hope I could explain it clearly enough...
精彩评论