在AppDelegate中的UI引导代码中选择根视图控制器(vs viewDidLoad)

时间:2016-09-22 22:04:21

标签: ios swift swift2

如果我问一些愚蠢的话,请原谅我(iOS和swift的新手)。我试图在本教程视频的帮助下集成MMDrawerController。

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

在本教程中,以下代码添加到app delegate

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(),以为这个代码只是连接和启动类但我认为这出错了。

此外,为了您的信息,我通过此代码从集合视图中点击一个单元格来唤起centerViewController。

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

现在,只要调用此代码,我的应用程序就会冻结,应用程序的RAM使用量会持续增加,直到崩溃。

我将日志放在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.

1 个答案:

答案 0 :(得分:0)

UIWindow / AppDelegate启动代码移至viewDidLoad()绝对是霰弹枪编码"编码"方法,实质上是要求View Controller在加载完成后立即重新加载。

在概念层面,您需要了解应用程序生命周期与视图控制器生命周期。苹果公司的开发者文章很不错。我会把搜索留给你。

相反,如果你要覆盖故事板启动机制(确保你知道自己正在进入什么状态 - 看起来你正在趟到沼泽地,你还没准备好),然后告诉App Delegate哪个View Controller是你想要的入口点。您正在查看的是"的代码版本是初始视图控制器" Storyboard中的复选框/右指向箭头(属性视图控制器的检查器)。当然,该代码现在忽略了这个Storyboard设置,因此您需要在代码中拼写出来。

所以它看起来像:

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

...

window.rootViewController = viewControllerInstanceThatIReallyWant

据推测,ClassOfViewControllerThatIReallyWant是故事板中VC的标识,并通过您设计的任何导航以某种方式引导相关的Category视图控制器。