0

I have the NSMutableArray property in ViewController.h Now i want to get this Array value from appdelegate. Is it possible can anyone help me to do that.

In viewcontroller.h

@property (nonatomic, retain) NSMutableArray *fullImg;

In viewcontroller.m

@synthesis fullImg;

I also assigned values for fullImg. Now i want to get that value in app delegate.

Thanks in advance.

8
  • Define the relationship between the app delegate and the view controller. Commented Jul 14, 2014 at 10:58
  • Store it in NSUserDefaults and then get it from where you want to use it. But trojanfoe is probably a better answer. Commented Jul 14, 2014 at 10:58
  • 2
    You should only have code in the AppDelegate which handles Application state. Mutable arrays which are needed in view controllers should not be in the AppDelegate. This is a symptom of bad architecture. Commented Jul 14, 2014 at 11:01
  • 1
    @dasdom @ HotLicks you're both right. The app delegate is the same as a singleton anyway. It's only created once in the app and you can get to it from anywhere. However, having said that, there are situations that using a singleton is valid. There is never a situation IMO when using the app delegate for anything than app state is valid. Having said that, I don't think either the app delegate or a singleton is good for this question. Both would be equally as bad ... in this situation. Commented Jul 14, 2014 at 16:35
  • 1
    @Fogmeister Thanks. And I totally agree. Commented Jul 14, 2014 at 19:25

4 Answers 4

1
  1. Move the array declaration from viewController to AppDelegate
  2. In viewController.m do:

top file: #import "AppDelegate.h"

in place you want to use the array:

AppDelegate *ap = [[UIApplication sharedApplication] delegate];
ap.fullImg // -> USE IT HOW YOU WANT 

...
Sign up to request clarification or add additional context in comments.

1 Comment

This is really bad architecture. The AppDelegate should never be holding application data.
0

You can put the whole NSMutableArray into NSUserDefaults with a key e.g.

In app delegate:

NSUserDefaults *def=[NSUserDefaults StanderUserDefaults];
[def setObject:fulImg forKey:@"ImageArray"];

In ViewController class:

NSUserDefaults *def=[NSUserDefaults StanderUserDefaults];
NSMutableArray *yourimgarry=[def valueForKey:@"ImageArray"];

Comments

0

When you create the object for viewController class, you can pass the array reference to the viewController by creating new custom constructor. 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;
}

Comments

0

There are two possibilities.

  1. Using appDelegate. Use a property in app delegate to pass data between ViewContriollers
    and AppDelegate.

    in first controller

    MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate]; appDelegate.fullImg=dataToPass;

    in the second controller

    MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate]; data=appDelegate.fullImg;

    in AppDelegate

    self.fullImg=data;

2.Specifying object Instance.

in your ViewController after allocating the AppDelegate , specify that the object instance in the current viewController is same as the one in AppDelegate. For this declare a NSMutableArray property in AppDelegate.

in AppDelegate.h

@property NSMutableArray *fullImg;

in ViewController.h

 MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate]; 

appDeleagte.fullImg=self.fullImg;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.