Ios 如何从appdelegate获取viewcontroller中NSMutableArray的值
我在ViewController.h中有NSMutableArray属性,现在我想从appdelegate获取这个数组值。 有没有人能帮我做那件事 在viewcontroller.h中Ios 如何从appdelegate获取viewcontroller中NSMutableArray的值,ios,objective-c,Ios,Objective C,我在ViewController.h中有NSMutableArray属性,现在我想从appdelegate获取这个数组值。 有没有人能帮我做那件事 在viewcontroller.h中 @property (nonatomic, retain) NSMutableArray *fullImg; MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate]; appDeleagte.fullImg=self.fu
@property (nonatomic, retain) NSMutableArray *fullImg;
MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate];
appDeleagte.fullImg=self.fullImg;
在viewcontroller.m中
@synthesis fullImg;
我还为fullImg赋值。现在我想在app delegate中获得该值
提前谢谢
AppDelegate *ap = [[UIApplication sharedApplication] delegate];
ap.fullImg // -> USE IT HOW YOU WANT
...
为
viewController
类创建对象时,可以通过创建新的自定义构造函数将数组引用传递给viewController
。i、 e
//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 在第一控制器中 MyAppDelegate appDelegate=[[UIApplication sharedApplication]委托]; appDelegate.fullImg=dataToPass 在第二个控制器中 MyAppDelegate appDelegate=[[UIApplication sharedApplication]委托]; 数据=appDelegate.fullImg 在AppDelegate中 self.fullImg=数据
@property NSMutableArray *fullImg;
在ViewController.h中
@property (nonatomic, retain) NSMutableArray *fullImg;
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"];
定义应用程序委托和视图控制器之间的关系。将其存储在NSUserDefaults中,然后从要使用它的位置获取。但特洛伊木马可能是一个更好的答案。您应该在AppDelegate中只有处理应用程序状态的代码。视图控制器中需要的可变数组不应位于AppDelegate中。这是糟糕架构的症状。@dasdom@HotLicks你们都是对的。无论如何,应用程序委托与单例委托相同。它只在应用程序中创建一次,您可以从任何地方访问它。然而,尽管如此,在某些情况下使用单例是有效的。在使用应用程序委托进行任何操作时,都不会出现应用程序状态无效的情况。话虽如此,我认为无论是应用程序委派还是单身都不适合这个问题。两者都一样糟糕。。。“在这种情况下,”福格迈斯特,谢谢。我完全同意。这真是糟糕的建筑。AppDelegate不应保存应用程序数据。