我正在使用 Storyboard在 View 之间切换。非常简单,直到我尝试添加 ECSlidingViewController。
如果我将上面的幻灯片菜单添加到我使用此调用的第一个 View :
self.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Main"];
如果我将@“Main”设置为@“Speakers”,则 View 加载得很好。我得到了幻灯片菜单和一切。 @“Main”也加载得很好。
但是,如果我像上面的代码一样首先加载@“Main”,然后将 View 切换到我指定的“Speakers”,那么当我尝试使用以下代码调用幻灯片菜单时,它就会崩溃:
if(![self.slidingViewController.underLeftViewController isKindOfClass:[MenuViewController class]]) {
self.slidingViewController.underLeftViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Menu"];
}
[self.view addGestureRecognizer:self.slidingViewController.panGesture];
我收到崩溃消息:* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“* -[__NSArrayM insertObject:atIndex:]:对象不能为 nil”
我尝试过多种方式切换 View 。
我试过:
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];
如果我不调用幻灯片菜单,每个都可以正常工作,但当我添加滑动手势时,每个都会导致崩溃。
想法?
最佳答案
好吧,我明白了。经过长期艰苦的经历,我想通了。
所以我有很多文件(每个文件都有 .m 和 .h):
初始化 View Controller
菜单 View Controller
主视图 Controller
演讲者 View
目标是使用幻灯片菜单上没有的按钮从 MainViewController 切换到SpeakersView,但正如 Phillip Mills 指出的那样,直接这样做会导致幻灯片菜单为空。
相反,我把它放在 InitViewController.h 中,就在 @Interface 下面:
@property (nonatomic, retain) NSString *location;
然后在 InitViewController.m 中的 @implementation 下:
@synthesize location;
我最初在 InitViewController.m 的 ViewDidLoad 下有这个:
self.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Main"];
我把它改成这样:
if([location length] == 0){location = @"Main";}
self.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:location];
这样,如果变量为空,则默认为@“Main”。
然后在 MainView.m 中,按下按钮后执行代码,我有以下内容:
InitViewController *ini;
ini = [self.storyboard instantiateViewControllerWithIdentifier:@"Init"];
ini.location = @"Speakers";
瞧, View 切换到扬声器就好了。更准确地说,它切换到 InitViewController,它会自动切换到扬声器或我设置的位置。
感谢大家的帮助。
关于iOS 添加菜单会导致非首次查看时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18089977/