如何从源代码中正确打开新控制器?

时间:2016-08-19 13:18:52

标签: ios swift

两周前,我开始使用swift和iOS开始我的旅程。我有记忆问题。最初,登录后,我的应用程序消耗30 MB。然后用户可以从菜单中选择四个窗口。我使用以下代码在它们之间切换:

let controller = UIStoryboard(name:"Main", bundle:nil).instantiateViewControllerWithIdentifier(content[indexPath.row]) as! UINavigationController
self.presentViewController(controller, animated: true, completion: nil)

每当我从菜单中选择另一个选项并出现新窗口时,app会消耗另外1 MB的内存。经过多次更改后,应用程序消耗了90 MB(已签入xcode仪器)。我确定这段代码有问题。我应该以某种方式解雇旧窗口吗?

1 个答案:

答案 0 :(得分:2)

使用' presentViewController'你只是模态地向堆栈添加更多视图,如果想要在这4个视图之间切换,TabBarController应该是要使用的组件。

另一方面,如果您只想更改初始视图控制器以在这些视图之间切换,则应通过将以下内容添加到AppDelegate来更改根视图控制器

func setRootViewController(viewController: UIViewController) {
    self.window?.rootViewController = viewController
}

在需要时使用以下内容在控制器之间切换

let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
        appDelegate?.setRootViewController(otherViewController)

希望有所帮助:)