[英]How to get the value of NSMutableArray in viewcontroller from the appdelegate
我在ViewController.h中具有NSMutableArray屬性,現在我想從appdelegate獲取此Array值。 有沒有人可以幫助我做到這一點。
在viewcontroller.h中
@property (nonatomic, retain) NSMutableArray *fullImg;
在viewcontroller.m中
@synthesis fullImg;
我還為fullImg分配了值。 現在,我想在應用程序委托中獲得該值。
提前致謝。
頂級文件:#import“ AppDelegate.h”
您要使用數組的位置:
AppDelegate *ap = [[UIApplication sharedApplication] delegate];
ap.fullImg // -> USE IT HOW YOU WANT
...
為viewController
類創建對象時,可以通過創建新的自定義構造函數將數組引用傳遞給viewController
。 即
//in appdidFinishLaunchingWithOptions
ViewController *viewController = [[ViewController alloc] initWithArray:fullImgArray];
self.window.rootViewController = viewController;
// In ViewController.h
- (id)initWithArray:(NSMutableArray *)fullImgArray;
// In ViewController.m
- (id)initWithArray:(NSMutableArray *)fullImgArray
{
self = [super init];
if (self)
{
self.fulImg = fullImgArray;
}
return self;
}
有兩種可能性。
使用appDelegate。 使用應用程序委托中的屬性在ViewContriollers之間傳遞數據
和AppDelegate。
在第一個控制器中
MyAppdeleagte appDelegate = [[UIApplication sharedApplication] delegate]; appDelegate.fullImg = dataToPass;
在第二個控制器中
MyAppdeleagte appDelegate = [[UIApplication sharedApplication] delegate]; 數據= appDelegate.fullImg;
在AppDelegate中
self.fullImg =數據;
2.指定對象實例。
在分配AppDelegate之后,在ViewController中,指定當前viewController中的對象實例與AppDelegate中的對象實例相同。 為此,在AppDelegate中聲明NSMutableArray屬性。
在AppDelegate.h中
@property NSMutableArray *fullImg;
在ViewController.h中
MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate];
appDeleagte.fullImg=self.fullImg;
您可以使用密鑰將整個NSMutableArray放入NSUserDefaults中,例如
在應用程序委托中:
NSUserDefaults *def=[NSUserDefaults StanderUserDefaults];
[def setObject:fulImg forKey:@"ImageArray"];
在ViewController類中:
NSUserDefaults *def=[NSUserDefaults StanderUserDefaults];
NSMutableArray *yourimgarry=[def valueForKey:@"ImageArray"];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.