0

I firstly apologize for the question title which probably doesn't make much sense or is super obvious. I have a tabbarcontroller with two views on it (view1 and view2). It starts off at view1 as initial view. I need to pass something from view1 to view2 which I have figured out thanks to this awesome community =]. However when I run the simulator, as I start off on view1 if i pass the object then and there, nothing happens but if I go to "see" view2 first and then move back to view1, the object is received perfectly. I think this is an issue of view2's array2 not existing at first until I actually ask to see it since it is initialized in viewDidLoad. Any help is appreciated. Thanks.

this is my viewDidLoad in view2:

[super viewDidLoad];
// Do any additional setup after loading the view.

self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

self.array2 =[[NSMutableArray alloc]init];

this is the method of a button push in view1 that gives the object to view2:

            object = [self.array1 objectAtIndex:15];
            UINavigationController *navi = (UINavigationController *)[[self.tabBarController viewControllers] objectAtIndex:1];
            SecondViewController *secViewController = [[navi viewControllers] objectAtIndex:0];

            [secViewController.array2 addObject:object];
            [secViewController.tableView reloadData];

I "think the problem is that I should init the array2 earlier than viewDidLoad and Im guessing this would be in the init of the viewController. I do not know how to do this, my VC2.m does not have an init method and Im not sure as how to make it init the array2 before I go to this screen.

snapfish
  • 175
  • 1
  • 14
  • 1
    It would help if you post the codes you are using in order to offer some solution. – Adrian P Feb 28 '14 at 03:47
  • ^updated, please let me know if you need other code. thanks – snapfish Feb 28 '14 at 04:02
  • What do you mean by "nothing happens"? What makes you think this object was not passed to a view controller? What did you expect to happen and what did you see instead? Perhaps most importantly, how can you test your assumptions? – Jonah Feb 28 '14 at 04:04
  • I placed an initial object:dummy in the array2 of vc2. when the order of events go: run simulator, vc1(this is initial screen), send object to array2, vc2. All i see is the dummy I initialized vc2 with. when the order of events go: run simulator, vc1, vc2(i see dummy here), vc1, send object to array2, vc2(i now see dummy and new object) – snapfish Feb 28 '14 at 04:11
  • 1
    Your analysis of the problem is correct. So what exactly do you not know how to do? Write an init method? Which init method you use depends on how the controllers are created. If you made this in a storyboard, then you should use initWithCoder, if in a xib file, use initWithNibName:bundle:, if in code, just init. – rdelmar Feb 28 '14 at 05:29
  • I do not know where to write the method initWithCoder (inside implementation of vc2.m?) and what to write in it. Sorry if this is basic but I just started iOS and have only done basic stuff. I looked up initWithCoder but the results r a bit confusing like [here](http://stackoverflow.com/questions/3943801/objective-c-how-do-i-use-initwithcoder-method) do i need an encoder method as well in vc2.m? – snapfish Feb 28 '14 at 06:27
  • @rdelmar could you please let me know how to use initWithCoder in this situation? I'm confused as to what to encode it with as show here [link](http://stackoverflow.com/questions/3943801/objective-c-how-do-i-use-initwithcoder-method). Do i need to use a encoder method at all? Thank you so much in advance. – snapfish Mar 01 '14 at 22:41

2 Answers2

1

Use initWithCoder: since your controllers are made in IB. Put this code in the vc2 .m file.

-(id)initWithCoder:(NSCoder *) aDecoder {
   if (self = [super initWithCoder: aDecoder]) {
        _array2 =[[NSMutableArray alloc]init];
   }
   return self;
}
rdelmar
  • 103,982
  • 12
  • 207
  • 218
0

In Tab View Controller, view load only when that view is visible. So for the first time when your app lunches, it loads only first view. Second view won't be load. So your -viewDidLoad of second view controller will not called. Hence your array is not initiated at this time.

So you need to allocate array in init method.

 -(id)init
{
   self = [super init];
   if(self) 
   {
        _array2 =[[NSMutableArray alloc]init];
   }
   return self;
}

I think it will work, if not write this line in other init methods.

Rajath Shetty K
  • 424
  • 3
  • 9
  • when I write this in vc2, there's no longer a tableview showing up in vc2 anymore at all. it's just the view with navBar and tabBar. I assume u are correct in your approach but how come my tableView in vc2 only comes up when I init the array2 in viewDidLoad? The vc2 tableview is listed as a property (strong,nonatomic) IBOutlet in vc2. – snapfish Feb 28 '14 at 17:33