2016-09-22 19 views
2

请原谅我是否问了一些愚蠢的问题(对iOS和swift而言是新的)。我正在尝试在本教程视频的帮助下整合MMDrawerController。在AppDelegate(vs viewDidLoad)中选择UI引导代码中的根视图控制器

https://www.youtube.com/watch?v=c-Uwa5v_3sc

在本教程中,下面的代码添加到应用程序委托

var window :UIWindow = UIApplication.sharedApplication().keyWindow! 
     let rootViewController = window.rootViewController 
     let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
     let centerViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Category") as! Category 
     let leftViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Menu") as! Menu 

     let leftSideNav = UINavigationController(rootViewController: leftViewController) 
     let centerNav = UINavigationController(rootViewController: centerViewController) 

     centerContainer = MMDrawerController(centerViewController: centerNav, leftDrawerViewController: leftSideNav) 
     centerContainer!.openDrawerGestureModeMask = MMOpenDrawerGestureMode.PanningCenterView; 
     centerContainer!.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.PanningCenterView; 
     window.rootViewController = centerContainer 
     window.makeKeyAndVisible() 

但是当我做,它使应用程序开始centerViewController,但我不认为有什么行为,所以我将该代码移动到centerViewController的viewDidLoad()上,以为该代码只是连接并启动类,但我认为这出错了。

此外,为了您的信息,我正在通过此代码从集合视图的单元格的tap上调用centerViewController。

var DestViewController = segue.destinationViewController as! UINavigationController 
      let targetController = DestViewController.topViewController as! Category 

现在,只要该代码被称为我的应用程序冻结和应用程序的内存占用不断,直到崩溃增加。

我把日志放在viewDidLoad()中,发现它是递归调用的。任何人都可以请建议我如何解决这个问题。

谢谢。

更新:

的viewDidLoad功能是这样的

override func viewDidLoad() { 
     super.viewDidLoad() 
     // 
     print("Inside Category") 
     var window :UIWindow = UIApplication.sharedApplication().keyWindow! 
     print("1") 
     let rootViewController = window.rootViewController 
     print("2") 
     let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
     print("3") 
     let centerViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Category") as! Category 
     print("4") 
     let leftViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Menu") as! Menu 

     let leftSideNav = UINavigationController(rootViewController: leftViewController) 
     let centerNav = UINavigationController(rootViewController: centerViewController) 

     centerContainer = MMDrawerController(centerViewController: centerNav, leftDrawerViewController: leftSideNav) 
     centerContainer!.openDrawerGestureModeMask = MMOpenDrawerGestureMode.PanningCenterView; 
     centerContainer!.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.PanningCenterView; 
     window.rootViewController = centerContainer 
     print("5") 
     // window.makeKeyWindow() 
     window.makeKeyAndVisible() 
     print("6") 
     // 
     let tapGestureRecognizerCat = UITapGestureRecognizer(target:self, action:#selector(Category.btnCategoryTapped(_:))) 
     btnCategory.userInteractionEnabled = true 
     btnCategory.addGestureRecognizer(tapGestureRecognizerCat) 
     // 
     let tapGestureRecognizerSearch = UITapGestureRecognizer(target:self, action:#selector(Category.btnSearchTapped(_:))) 
     btnSearch.userInteractionEnabled = true 
     btnSearch.addGestureRecognizer(tapGestureRecognizerSearch) 
     print("7") 
    } 

堆栈跟踪

Inside Category 
1 
2 
3 
4 
5 
6 
7 
Inside Category 
1 
2 
3 
4 
5 
6 
7 
Inside Category 
1 
2 
3 
4 
5 
6 
7 ... and keeps on going like this. 
+0

用递归调用的'viewDidLoad'方法更新你的问题。同时发布显示递归的堆栈跟踪。 – rmaddy

+0

@rmaddy问题已更新。 –

+1

你为什么要用'viewDidLoad'中的窗口做任何事情?这是没有意义的。 'viewDidLoad'只能进行视图控制器视图所需的任何设置。而已。不要碰那里的窗户。 – rmaddy

回答

0

移动UIWindow/AppDelegate的启动代码viewDidLoad()绝对是 “编码由散弹枪” 的方针,实质上要求View Controller在完成加载后自行重新加载。

在概念级别,您需要了解应用程序生命周期与View Controller生命周期。有这样的体面的苹果开发者文章。我会把搜索留给你。

相反,如果您要覆盖Storyboard的启动机制(请确保您知道您正在进入的内容 - 看起来像是涉足沼泽地,但您没有准备好),然后告诉App Delegate哪个视图控制器是您想要的入口点。您正在查看的是Storyboard(View Controller的属性检查器)中的“is Initial View controller”复选框/右箭头的代码版本。当然,这段代码现在忽略了这个Storyboard设置,所以你需要在代码中进行拼写。

因此,它看起来像:

let viewControllerInstanceThatIReallyWant = mainStoryboard.instantiateViewControllerWithIdentifier("TheStoryBoardIDofTheViewControllerThatIReallyWant") as! ClassOfViewControllerThatIReallyWant 

... 

window.rootViewController = viewControllerInstanceThatIReallyWant 

想必,ClassOfViewControllerThatIReallyWant是一个VC的故事板中的身份,并以某种方式通过你设计任何导航导致相关Category视图控制器。

+0

我一定会寻找文件。 :) –

相关问题