iOS Adding Menu Causes Crash When Not First View

邮差的信 提交于 2020-01-07 05:48:07

问题


I am using a storyboard to switch between views. Pretty simple, until I try to add ECSlidingViewController.

If I add the above slide menu to the first view I call using this:

self.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Main"];

If I set @"Main" to @"Speakers", the view loads just fine. I get the slide menu and everything. @"Main" also loads just fine.

However, if I load @"Main" first like in the code above, then switch views to the one I've designated "Speakers", it crashes as soon as I try to call the slide menu with this code:

   if(![self.slidingViewController.underLeftViewController isKindOfClass:[MenuViewController class]]) {
    self.slidingViewController.underLeftViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Menu"];
   }

   [self.view addGestureRecognizer:self.slidingViewController.panGesture];

I get a crash stating: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

I have tried switching views numerous ways.

I've tried:

SpeakersView *second= [self.storyboard instantiateViewControllerWithIdentifier:@"Speakers"];
[self presentViewController:second animated:YES completion:nil];

I've tried:

  SpeakersView *svc = [self.storyboard instantiateViewControllerWithIdentifier:@"Speakers"];

[self presentViewController:svc animated:YES completion:nil];

Each one works just fine if I don't call up the slide menu, but each causes a crash when I do add the slide gesture.

Ideas?


回答1:


Okay, I figured it out. After a long, arduous experience, I figured it out.

So I've got numerous files (.m and .h for each):

InitViewController

MenuViewController

MainViewController

SpeakersView

The objective was to switch from MainViewController using a button not on the slide menu to SpeakersView, but doing it directly caused the slide menu to be null, as was pointed out by Phillip Mills.

Instead, I put this in InitViewController.h, right underneath @Interface:

@property (nonatomic, retain) NSString *location;

Then this under @implementation in InitViewController.m:

@synthesize location;

I originally had this under ViewDidLoad in InitViewController.m:

self.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Main"];

I changed it to this:

if([location length] == 0){location = @"Main";}
self.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:location];

That way if the variable was empty, it defaulted to @"Main".

Then in MainView.m, where I execute the code after pressing the button, I have this:

InitViewController *ini;
ini = [self.storyboard instantiateViewControllerWithIdentifier:@"Init"];
ini.location = @"Speakers";

And voilà, the view switches just fine to Speakers. More accurately, it switches to InitViewController which automatically switches to Speakers or whatever I set location to.

Thanks to everyone for your help.



来源:https://stackoverflow.com/questions/18089977/ios-adding-menu-causes-crash-when-not-first-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!