-1

I have two view controllers. We'll call them listViewController and mapViewController. The listViewController shows first, a user taps a button that shows the mapViewController. The user taps a button on a annotation which is where my problems start.

When the user taps a button on mapViewController, I would like to call a method listViewController and dismiss mapViewController. To do this I am using this code:

mapViewController.m

listViewController* lvc = [[listViewController alloc] initWithNibName:nil bundle:nil];
[lvc getInformation:StringParameter];
[self dismissViewControllerAnimated:YES completion:nil];

However, when executing getInformation it seems that the listViewController class has been dealloc'd because all of the objects that I initialized in the viewDidLoad on listViewController are now nil including self, which just breaks everything.

I assume I'm creating the listViewController object incorrectly in mapViewController. I've tried it with a nil for nibName with the same result.

ecnepsnai
  • 1,882
  • 4
  • 28
  • 56
  • You need to use a delegate pattern: http://stackoverflow.com/questions/8606674/uimodaltransitionstylepartialcurl-doesnt-get-back-to-previous-state-not-dismi – user523234 Jul 11 '14 at 03:30
  • Very similar question with several correct answers that will probably solve your issue http://stackoverflow.com/questions/24457936/warning-attempt-to-present-whose-view-is-not-in-the-window-hierarchy/24459150#24459150 – Brandon Jul 11 '14 at 03:55

2 Answers2

1

Ok, Let me clear one thing. On listViewController, you presentViewController a mapViewcontroller right? And you want invoke getInformation of the instance from mapViewController. In the code you're added, you instantiate listViewController again. You have 2 different instance of listViewController.

Ryan
  • 4,799
  • 1
  • 29
  • 56
1

One option is to use a delegate pattern:

In your MapViewController.h file:

@protocol MapViewControllerDelegate <NSObject>
-(void)dismissMe;
-(void)getInformation:(NSString *)stringParameter;

@end

@interface MapViewController : UIViewController
@property (weak)id <MapViewControllerDelegate> delegate;
@end

In your MapViewController.m file:

-(void)annotationButtonTap:(id)button
{
    [self.delegate getInformation:stringParameter];
    [self.delegate dismissMe];
}

In your ListViewController.h

#import "MapViewController.h"

@interface ListViewController : UIViewController <MapViewControllerDelegate>

-(void)dismissMe
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)getInformation:(NSString *)stringParameter
{
    //do whatever you plan with stringParameter
}

And somewhere in you ListViewController.m file where you create the MapViewController instance:

mapViewController = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil];
mapViewController.delegate = self;   //make sure you do this
user523234
  • 14,323
  • 10
  • 62
  • 102