1

I know that this question has been asked several times already, but after extensive search, none of them work for me so far. I want to avoid using a singleton if possible. Sorry if this is considered a repeat question.

I have a MutableArray with 5 objects in my ViewController.m file called storyList. I want to be able to access this storyList array in my DetailViewController.m file.

One method I have tried is: (storyListArrayinDetail is a new array in detailviewcontroller, storyList is the array with 5 objects from ViewController.h) In ViewController.h

DetailViewController *controller;

In ViewController.m

 controller = [[DetailViewController alloc]init];
 controller.storyListArrayinDetail = [[NSMutableArray alloc]init];       
 controller.storyListArrayinDetail = storyList;

When I log controller.storyListArrayinDetail in ViewController.m, I get the correct array of 5 objects. However, when I log storyListArrayinDetail in DetailViewController.m, I get nothing.

Charles
  • 50,943
  • 13
  • 104
  • 142
Spenciefy
  • 892
  • 11
  • 33

2 Answers2

0

Try overriding the method (id)init on DetailViewController.m ,and initialize storyListArrayinDetail here. Then, whenever initializing a DetailViewController you will be initializing storyListArrayinDetail as well. So, for sending your storyList just use your code as this:

controller = [[DetailViewController alloc]init];
controller.storyListArrayinDetail = storyList;

Hope that helps

CoderPug
  • 908
  • 9
  • 19
  • How do I use the array `controller.storyListArrayinDetail` in DetailViewController? There is no controller defined in detailViewController – Spenciefy Apr 03 '13 at 23:17
  • This is what I meant, on `DetailViewController.m` override init method and initialize `storyListArrayinDetail` like this: `- (id)init { self = [super init]; if (self) { storyListArrayinDetail = [[NSMutableArray alloc]init]; } return self; }` No need to use `controller`, because you are inside `DetailViewController` class. – CoderPug Apr 04 '13 at 16:17
0

You can create a new init method in your DetailViewController and pass in the array when you initialize it.

//Assuming DetailViewController is in fact a UIViewController, not sure from init method in OP.

DetailedViewController.h

@interface DetailedViewController : UIViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withDataArray:(NSArray*)dataArray;
@end

DetailedViewController.m

@interface DetailedViewController () {
   NSArray *myDataArray;
}

@end

@implementation DetailedViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withDataArray:(NSArray*)dataArray
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
            myDataArray = [NSArray arrayWithArray:dataArray];
        }
        return self;
    }

...

Then in DetailedViewController you can use myDataArray. Hope that helps.

EDIT: To include Hot_Licks suggestion:

DetailedViewController.h

@interface DetailedViewController : UIViewController
@property(nonatomic, assign)NSArray *myDataArray;
@end

ViewController.m

DetailedViewController *controller = [[DetailViewController alloc]init];
[controller setMyDataArray:xyArray];
random
  • 8,568
  • 12
  • 50
  • 85
  • Or you can add a property to the controller and set it with the array address after the controller is created. – Hot Licks Apr 04 '13 at 02:16
  • Thanks for the answer, and I think it should work, but when I put `- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withDataArray:(NSArray*)dataArray;` in `DetailViewController.h`, I get 2 errors: `Expected member name or '; ' after declaration specifiers` and `Type name requires a specifier or qualifier` – Spenciefy Apr 04 '13 at 05:50
  • Is you DetailViewController a UIViewController? Or a UIView? – random Apr 04 '13 at 23:19