问题
I have a quite big NSArray
, that i have to use in several view controllers. I get the data from a server and i don't wanna download it always when the user opens a new view.
So how can i define it as a "global variable"?
I can do it with segues, but without them it's not working. I've tried to declare an array called dataToDestinationView
in the DestinationViewController
, then imported it to the view where the self.arrayToExport
is and add the lines above, but doesn't worked. Do i missed something?
DestinationViewController *dataToNew = [DestinationViewController alloc]init];
dataToNew.dataToDestinationView = self.arrayToExport;
UPDATED with my try in AppDelegate:
AppDelegate.h
@property (nonatomic, strong) PNChannel *myChannel;
AppDelegate.m
[PubNub requestHistoryForChannel:self.myChannel from:nil to:nil limit:100 reverseHistory:NO withCompletionBlock:^(NSArray *contentArray, PNChannel *channel, PNDate *fromDate, PNDate *toDate, PNError *error) {
+ (NSArray *)mySharedArray
{
static NSArray *sharedArray = nil;
if (sharedArray)
return sharedArray;
sharedArray = contentArray;
return sharedArray;
}
}];
In this case i get two errors: Use of undeclared identifier 'self'
and Expected identifier or '('
. I don't understand it, because self.myChannel is declared in the .h and i'm using the same block elsewhere without the identifier problem.
SECOND UPDATE
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// [self.window makeKeyAndVisible];
[PubNub setDelegate:self];
PFUser *currentChannel = [PFUser currentUser];
self.myChannel = [PNChannel channelWithName:currentChannel.username
shouldObservePresence:YES];
}
- (void)getMessage {
[PubNub requestFullHistoryForChannel:self.myChannel withCompletionBlock:^(NSArray *contentArray, PNChannel *channel, PNDate *fromDate, PNDate *toDate, PNError *error) {
+ (NSArray *)mySharedArray
{
static NSArray *sharedArray = nil;
if (sharedArray)
return sharedArray;
sharedArray = contentArray;
return sharedArray;
}
NSLog(@"mokus katona %@", contactArray);
}];
}
回答1:
Inside your app delegate, declare a static variable, and a method to read it:
static NSArray *_message = nil;
@implementation AppDelegate
+ (NSArray *)message
{
if (_message)
return _message;
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate getMessage];
return nil;
}
- (void)getMessage {
[PubNub requestFullHistoryForChannel:self.myChannel withCompletionBlock:^(NSArray *contentArray, PNChannel *channel, PNDate *fromDate, PNDate *toDate, PNError *error) {
NSLog(@"mokus katona %@", contactArray);
_message = contactArray;
}];
}
@end
回答2:
Maybe this can help you.
In your pch file (#project-Prefix.pch) you can create your global variable, available from all spaces. Look below
#ifdef __OBJC__
NSArray *myArray;
#endif
and in your app delegate, in your - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
, you can fill your array with the server data like this
[PubNub requestFullHistoryForChannel:self.myChannel withCompletionBlock:^(NSArray *contentArray, PNChannel *channel, PNDate *fromDate, PNDate *toDate, PNError *error) {
NSLog(@"mokus katona %@", contactArray);
myArray = contactArray;
}];
Now this is only one time loading and myArray
will be available in all the controllers, models, view etc.
Let me know if this helps.
来源:https://stackoverflow.com/questions/24355605/access-an-nsarray-from-every-view-controller