Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从appdelegate获取viewcontroller中NSMutableArray的值_Ios_Objective C - Fatal编程技术网

Ios 如何从appdelegate获取viewcontroller中NSMutableArray的值

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

我在ViewController.h中有NSMutableArray属性,现在我想从appdelegate获取这个数组值。 有没有人能帮我做那件事

在viewcontroller.h中

@property (nonatomic, retain) NSMutableArray *fullImg;
 MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate]; 

appDeleagte.fullImg=self.fullImg;
在viewcontroller.m中

@synthesis fullImg;
我还为fullImg赋值。现在我想在app delegate中获得该值

提前谢谢

  • 将数组声明从viewController移动到AppDelegate
  • 在viewController.m中,执行以下操作:
  • 首要文件: #导入“AppDelegate.h”

    要使用阵列的位置:

    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。使用应用程序中的属性委托在ViewController之间传递数据
    和AppDelegate

    在第一控制器中

    MyAppDelegate appDelegate=[[UIApplication sharedApplication]委托]; appDelegate.fullImg=dataToPass

    在第二个控制器中

    MyAppDelegate appDelegate=[[UIApplication sharedApplication]委托]; 数据=appDelegate.fullImg

    在AppDelegate中

    self.fullImg=数据

  • 2.指定对象实例

    分配AppDelegate后,在ViewController中,指定当前ViewController中的对象实例与AppDelegate中的对象实例相同。为此,请在AppDelegate中声明NSMutableArray属性

    在AppDelegate.h中

    @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不应保存应用程序数据。