我有一个 _MainViewController,在它的 View 中,我有 2 个 subview ,
- _MenuViewController.view 显示菜单并且总是在前面。
- _MenuViewController.view 后面的另一个 View 发生了变化 根据在菜单中选择的内容。所以 _MenuViewController.view 是一个叠加层。
现在,为了改变后面的 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) storyboard_id=@"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。 这是 View Controller 的实例(全局变量):
UIViewController *_MyViewController;
现在,按钮的 Action :
- (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。
最佳答案
你不应该使用一个 ViewController 并将它的 View 更改为一种导航,这不是 View Controller 的目的。 每个(根) View 都应该有自己的 Controller 。使用导航 Controller 或其他东西,我认为您的导航做得不是很好。
关于ios - 在 iOS 中设置全局变量后,我的应用程序崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23518113/