【发布时间】:2015-04-04 09:38:46
【问题描述】:
简短的解释。
我有一个 ContainerViewController 正在推送到 navigationStack。
ContainerViewController 有 2 个子 ViewController。 SlidePanelViewController(滑出式菜单)和CenterViewController(内容)
我的菜单中有一个“退出”按钮。单击此按钮时,我想将ContainerViewController(它是2 childViewControllers)推送到我的LandingPageViewController。
这是我要调用的函数:
func signOut() {
println("signOut")
// Set up the landing page as the main viewcontroller again.
let mainTableViewController = LandingPageVC()
mainTableViewController.navigationItem.setHidesBackButton(true, animated: false)
mainTableViewController.skipView = false
self.navigationController!.pushViewController(mainTableViewController, animated: true)
// Disable menu access
menuEnabled = false
// change status bar style back to default (black)
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default
}
起初我尝试将其放入我的SlidePanelViewController。那没有用。所以我把它放在我假设它属于ContainerViewController的地方。
但是,当我在菜单中单击 signOutButton 时。我收到了错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
查看错误时。这是导致它的行:
self.navigationController!.pushViewController(mainTableViewController, animated: true)
在出现错误后,我通过添加一个调用该函数的UINavigationBarButtonItem(在我的ContainerViewController 中)来检查该函数是否有效。它完全符合我的要求。
但是,当我从菜单中调用此函数时(同样,我的菜单是 childViewController 的 ContainerViewController)。它不起作用。
我试图这样称呼它:
ContainerViewController().signOut()
我还尝试将Delegate 添加到我的SidePanelViewController,如下所示:
课前:
@objc protocol SidePanelViewControllerDelegate {
optional func needsSignOut(sender: SidePanelViewController)
optional func toggleLeftPanel()
optional func collapseSidePanels()
}
在viewDidLoad():
// Make sure your delegate is weak because if a ContainerViewController owns
// a reference to a SidePanelViewController and the container view controller
// is its delegate, you'll end up with a strong reference cycle!
weak var delegate: SidePanelViewControllerDelegate?
在我的点击手势功能中:
func signOutTapGesture() {
println("signOutTapGesture")
selectView(signOutView)
delegate?.needsSignOut?(self)
println(delegate)
}
在我的ContainerViewController 上课之前:
var leftViewController: SidePanelViewController?
我的ContainerViewController 班级:
class ContainerViewController: UIViewController, CenterViewControllerDelegate, SidePanelViewControllerDelegate, UIGestureRecognizerDelegate {
在我的ContainerViewController'sviewDidLoad()
leftViewController?.delegate = self
我将ContainerViewController 类中的signOut 函数更改为:
func needsSignOut(sender: SidePanelViewController) {
println("needsSignOut called")
self.signOut()
}
但是使用上面的委托,似乎也没有做任何事情。
任何有关如何从菜单中成功推送我的 LandingPageVC 的帮助将不胜感激! (我没有使用故事板)
【问题讨论】:
-
我回答了这个 Stackoverflow 问题,我认为它可以帮助您构建应用程序:stackoverflow.com/questions/28640969/…
标签: ios swift uiviewcontroller menu uicontainerview