Frage

My app is crashing when trying to access a method in my parentViewController. Here is the layout in StoryBoard

enter image description here

MainViewController = STLMMainViewController (ParentViewController)

Scene1 = STLMTimeDateViewController (ChildViewController)

Here is the code for STLMTimeDateViewController

#import "STLMTimeDateViewController.h"
#import "STLMMainViewController.h"

@interface STLMTimeDateViewController ()
@property (nonatomic, strong) STLMMainViewController *stlmMainViewController;
@end


- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"The name of the controller %@",self.navigationController.parentViewController);

    stlmMainViewController= (STLMMainViewController *) self.parentViewController;
    [stlmMainViewController locationButtonSelected]; // This is where the App crashes
    NSLog(@"TimeDateController");
}

The App Runs, but when STLMMainViewController is called, the app crashes with the following error:

2013-02-10 16:33:57.422 MyApp[9120:c07] The name of the controller <STLMMainViewController: 0x83850d0>
2013-02-10 16:33:57.434 MyApp[9120:c07] -[UINavigationController locationButtonSelected]: unrecognized selector sent to instance 0x8371a70

If I remove the following line:

stlmMainViewController = (STLMMainViewController *) self.parentViewController;

and just leave

[stlmMainViewController locationButtonSelected];

The App runs, no error, but the following method in [STLMMainViewController locationButtonSelected] does not get called (I never see the log):

-(void)locationButtonSelected
{
    [LocationButton setSelected:YES];
    [eatDrinkbutton setSelected:NO];
    [timeCalButton setSelected:NO];
    [carButton setSelected:NO];
    [contactButton setSelected:NO];
    NSLog(@"LocationButtonSelected Method");
}

All the properties in the locationButtonSelected method and the method itself is declared in .h of STLMMainViewController for public access.

Thanks

War es hilfreich?

Lösung

You might try this:

self.stlmMainViewController= (STLMMainViewController *)self.navigationController.parentViewController;

(EDIT: actually, as someone else just pointed out, you might want to use presentingViewController instead.)

It looks like you had it right in the log message right before this. You want your navigation controller's parent in this case.

BTW, the reason you don't crash when you delete this line, is because you end up sending the locationButtonSelected to nil. That won't crash, but it also won't do anything either.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top