设置全局变量我有一个_MainViewController并在其看来,我有2子视图,我的应用程序崩溃后,我在的iOS
- _MenuViewController.view,显示菜单,并总是在前面。
- 另一种观点behined _MenuViewController.view根据什么已在菜单中被选中,其改变 。所以,_MenuViewController.view是一个覆盖。现在
,为了改变落后的看法,我创造了这个功能:
UIViewController * ReplaceSubViews(UIViewController *present,NSString *vcid,NSString *storyboard_id)
{ //present: present 'behined' view,
//vcid: storyboard if of view to replace with behined
if(storyboard_id==nil) [email protected]"UI";
[present.view removeFromSuperview];
UIStoryboard *storybrd=[UIStoryboard storyboardWithName:storyboard_id bundle:nil];
UIViewController* future;
if(vcid==nil)
future= [storybrd instantiateInitialViewController];
else
future= [storybrd instantiateViewControllerWithIdentifier:vcid];
[_MainViewController.view addSubview:future.view];
[_MainViewController.view bringSubviewToFront:_MenuViewController.view];
return future;
}
现在,一个按钮事件touchUpInside调用该函数。 替换后,它将自己的viewcontroller的实例设置为nil。 这是视图 - 控制的实例(全局变量):
UIViewController *_MyViewController;
现在,按钮的动作:
- (IBAction)ShowARView:(id)sender {
ReplaceSubViews(self, nil, @"CamController");
double delayInSeconds = 2.0;
_MyViewController=nil; //App Crashes on this
}
我甚至试图通过延迟作废过程:
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
_MyViewController=nil; //This is basically self
});
但还是应用崩溃。请帮忙。 https://docs.google.com/drawings/d/1qqg7g2Hx3WAdC7SfGB4XpNDjuyNzZqFKlf55zED5x88/pub?w=272&h=256
我的代码启用ARC。
请从崩溃中显示堆栈跟踪。 –
你在控制台的错误说了什么? –
OMG!谁教过你们都从一个视图控制器获得了根视图并将其添加到另一个视图中?这从一开始就是失败的。 –