ios - 在 iOS 中设置全局变量后,我的应用程序崩溃了

标签 ios objective-c uiviewcontroller

我有一个 _MainViewController,在它的 View 中,我有 2 个 subview ,

  1. _MenuViewController.view 显示菜单并且总是在前面。
  2. _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/

相关文章:

iphone - 当 UITabBarController 的 'selectedViewController' 更改时触发事件?

ios - 禁用与 UIBarButtonItem 的用户交互

ios - 如何在 mapView 更改时刷新 MKOverlayRenderer

c# - CollectionView 与 WPF(或类似的东西)

ios - 是否可以通过编程方式将 Google 翻译集成到 iOS App 中(Swift 4)?

ios - stringByReplacingOccurrencesOfString 不适用于空间,即 objective-c 中的 ""

objective-c - 防止 CALayer 子层缩放

ios - 从表格 View 中删除不必要的行

objective-c - 在 UIScrollView 中裁剪 UIImage,如 "Move And Scale"

ios - 调用performSegueWithIdentifier后获取重复的UIViewController实例