简体   繁体   English

iOS添加菜单在非第一视图时导致崩溃

[英]iOS Adding Menu Causes Crash When Not First View

I am using a storyboard to switch between views. 我正在使用情节提要在视图之间切换。 Pretty simple, until I try to add ECSlidingViewController. 非常简单,直到我尝试添加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. 如果我将@“ Main”设置为@“ Speakers”,则视图加载就很好。 I get the slide menu and everything. 我得到了幻灯片菜单以及所有内容。 @"Main" also loads just fine. @“ Main”也可以加载。

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

   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' 我收到崩溃的消息: *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* -[__ NSArrayM insertObject:atIndex:]:对象不能为零'

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? 想法?

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): 所以我有很多文件(每个都有.m和.h):

InitViewController InitViewController

MenuViewController MenuViewController

MainViewController MainViewController

SpeakersView 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. 目的是使用不在幻灯片菜单上的按钮从MainViewController切换到SpeakersView,但这样做直接导致幻灯片菜单为空,如Phillip Mills所指出的那样。

Instead, I put this in InitViewController.h, right underneath @Interface: 相反,我将其放在@Interface下方的InitViewController.h中:

@property (nonatomic, retain) NSString *location;

Then this under @implementation in InitViewController.m: 然后在InitViewController.m中的@implementation下:

@synthesize location;

I originally had this under ViewDidLoad in InitViewController.m: 我最初在InitViewController.m中的ViewDidLoad下具有此功能:

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". 这样,如果变量为空,则默认为@“ Main”。

Then in MainView.m, where I execute the code after pressing the button, I have this: 然后在MainView.m中,在我按下按钮后执行代码,我得到了以下内容:

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. 更准确地说,它切换到InitViewController,后者会自动切换到Speakers或我设置的位置。

Thanks to everyone for your help. 感谢大家的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UILabel导致应用程序在添加到视图时崩溃(仅限Xcode 6和iOS 8) - UILabel causes app to crash when added to view (Xcode 6 and iOS 8 only) 取消视图时,performSelectorInBackground会导致随机崩溃 - performSelectorInBackground causes random crash when view is dismissing iOS应用程序更新并在第一次查看时崩溃 - iOS app update and crash on first view 推送/弹出第二个视图控制器后,iOS UI 导致崩溃 - iOS UI causes crash after pushing/popping second view controller iOS 9-网络可达性更改导致视图冻结-不会崩溃 - iOS 9 - Network reachability change causes the view to freeze - Not a crash 在viewcontroller中添加mapkit视图时崩溃 - crash when adding mapkit view in viewcontroller 与核心数据的反向关系在向NSSet添加对象时导致崩溃 - Inverse Relationship with Core Data Causes Crash when Adding Object to NSSet 现在添加自定义后退按钮会导致在进入RootViewController时崩溃 - adding a custom back button now causes crash when going to RootViewController 在横向时在 Apple Wallet 上添加卡片会导致崩溃 - Adding a card on Apple Wallet when in landscape causes a crash 设置视图控制器属性时(iOS)崩溃 - (iOS) crash when view controller property is set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM