从每个视图控制器访问 NSArray [英] Access an NSArray from Every View Controller
问题描述
我有一个相当大的 NSArray
,我必须在多个视图控制器中使用它.我从服务器获取数据,但我不想在用户打开新视图时总是下载它.
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"?
我可以用 segues 来做,但没有它们就行不通.我试图在 DestinationViewController
中声明一个名为 dataToDestinationView
的数组,然后将它导入到 self.arrayToExport
所在的视图中并添加上面的行,但不起作用.我错过了什么吗?
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;
更新了我在 AppDelegate 中的尝试:
AppDelegate.h
AppDelegate.h
@property (nonatomic, strong) PNChannel *myChannel;
AppDelegate.m
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;
}
}];
在这种情况下,我得到两个错误:Use of undeclared identifier 'self'
和 Expected identifier or '('
.我不明白,因为 self.myChannel 在 .h 中声明,我在其他地方使用相同的块而没有标识符问题.
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.
第二次更新
- (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);
}];
}
推荐答案
在你的应用委托中,声明一个静态变量,以及一个读取它的方法:
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
这篇关于从每个视图控制器访问 NSArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!