iOS添加菜单在非第一视图时会导致崩溃 [英] iOS Adding Menu Causes Crash When Not First View

查看:80
本文介绍了iOS添加菜单在非第一视图时会导致崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用情节提要在视图之间切换.非常简单,直到我尝试添加ECSlidingViewController.

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"];

如果我将@"Main"设置为@"Speakers",则视图加载就很好.我得到了幻灯片菜单以及所有内容. @"Main"也可以正常加载.

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

但是,如果像上面的代码那样首先加载@"Main",然后将视图切换到我指定的"Speakers",则当我尝试使用此代码调用幻灯片菜单时,它会崩溃:

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];

我崩溃了: *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* -[__ NSArrayM insertObject:atIndex:]:对象不能为空'

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.

我尝试过:

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

我尝试过:

  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.

想法?

推荐答案

好的,我知道了.经过漫长而艰苦的经历,我发现了.

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

所以我有很多文件(每个都有.m和.h):

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

InitViewController

InitViewController

MenuViewController

MenuViewController

MainViewController

MainViewController

SpeakersView

SpeakersView

目标是使用不在幻灯片菜单上的按钮从MainViewController切换到SpeakersView,但这样做直接导致幻灯片菜单为空,如Phillip Mills指出的那样.

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.

相反,我将其放在InitViewController.h中,位于@Interface的正下方:

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

@property (nonatomic, retain) NSString *location;

然后在InitViewController.m中的@implementation下:

Then this under @implementation in InitViewController.m:

@synthesize location;

我最初在InitViewController.m中的ViewDidLoad下拥有此功能:

I originally had this under ViewDidLoad in InitViewController.m:

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

我将其更改为:

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

这样,如果变量为空,则默认为@"Main".

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

然后在MainView.m中,我在按下按钮后执行代码,我得到了:

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";

瞧瞧,切换到议长就好了.更准确地说,它会切换到InitViewController,后者会自动切换到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.

感谢大家的帮助.

这篇关于iOS添加菜单在非第一视图时会导致崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆