簡短說明。從非活動的Viewcontroller推送到新的Viewcontroller(編程)
我有一個ContainerViewController
,我正在推送到navigationStack。
ContainerViewController
有2個子ViewControllers。 A 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
)錯誤後。它完全是我想要的。
但是,當我從我的菜單(我的菜單是ContainerViewController
的childViewController
)調用此功能。這是行不通的。
我試圖把它像這樣:
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's
viewDidLoad()
leftViewController?.delegate = self
我在ContainerViewController
類改變了signOut
功能如下:
func needsSignOut(sender: SidePanelViewController) {
println("needsSignOut called")
self.signOut()
}
但是使用委託像上面,似乎不要麼做任何事情。
任何幫助,如何我可以從菜單中成功地推動我的LandingPageVC將不勝感激! (我不使用故事板)
我給了這個Stackoverflow問題的答案,我認爲這可以幫助您構建您的應用程序:http://stackoverflow.com/questions/28640969/passing-view-controllers-with-facebook-login-confirmation/28669637#28669637 – Zhang 2015-04-04 15:48:32