2015-04-04 91 views
0

簡短說明。從非活動的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)錯誤後。它完全是我想要的。

但是,當我從我的菜單(我的菜單是ContainerViewControllerchildViewController)調用此功能。這是行不通的。

我試圖把它像這樣:

ContainerViewController().signOut() 

我也嘗試添加一個DelegateSidePanelViewController這樣的:

類前:

@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將不勝感激! (我不使用故事板)

+0

我給了這個Stackoverflow問題的答案,我認爲這可以幫助您構建您的應用程序:http://stackoverflow.com/questions/28640969/passing-view-controllers-with-facebook-login-confirmation/28669637#28669637 – Zhang 2015-04-04 15:48:32

回答

1

你試圖調用signOutContainerViewController().signOut()。這將創建一個新的ContainerViewController,因爲您尚未將其推入導航控制器的堆棧,因此navigationController爲零。嘗試撥打self.signOut()。 (我在ContainerViewController方法假設signOut

更新 - 代表

你的委託財產應當SidePanelViewController去。我給你,例如如何實現它:

SidePanelViewController: (注 - 該協議沒有去這裏,但我認爲它使組織的東西)

@objc protocol SidePanelViewControllerDelegate { 
    optional func needsSignOut(sender: SidePanelViewController) 
} 

class SidePanelViewController: UIViewController { 
    // 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? 

    // Called when the UIButton is pressed. 
    func myButtonWasPressed() { 
     delegate?.needsSignOut?(self) 
    } 
} 

ContainerViewController:

class ContainerViewController: UIViewController { 
    var sidePanel: SidePanelViewController! 
    // Setup the side panel... 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     sidePanel.delegate = self 
    } 

    func signOut() { 
     // Sign out stuff here. 
    } 
} 

// The ContainerViewController needs to conform to the SidePanelViewControllerDelegate 
// protocol if we want the delegate to work. (This could have gone in the initial 
// class declaration.) 
extension ContainerViewController : SidePanelViewControllerDelegate { 
    func needsSignOut(sender: SidePanelViewController) { 
     self.signOut() 
    } 
} 

希望有幫助。

+0

嘗試做'self.signOut()',但它會導致以下錯誤:''SidePanelViewController'沒有名爲'signOut''的成員 – 2015-04-04 10:53:51

+0

這個錯誤是因爲你試圖從一個實例調用'signOut' 'SidePanelViewController',它顯然不包含'signOut'方法。 'signOut'方法的確切位置? – ABakerSmith 2015-04-04 20:41:53

+0

'signOut'函數在'ContainerViewController'中。因此,我試圖調用'ContainerViewController()。signOut()'而不是'signOut()'。如果你問的是從哪裏調用signOut?需要運行該函數的按鈕位於我的'SidePanelViewController'中。 – 2015-04-04 22:56:21

0

問題似乎是navigationController是零,你試圖強制解包它(如你的錯誤所示)。

我在其他答案中討論過一個問題。

另一個問題可能是您沒有添加導航控制器。要做到這一點,你需要:

如果你使用故事板

你需要確保你已經嵌入你的UINavigationController。之後,當你使用navigationController它不會是零,你可以推動你的視圖控制器。

當你在你的故事板:

enter image description here

另外,如果你使用的故事板,你有沒有考慮使用塞格斯,而不是移動呼叫presentViewController的身邊?我發現它使一切變得更加容易。

如果你不使用故事板

看一看這個帖子:Programatically creating UINavigationController in iOS

+0

我不使用故事板。因此推到導航堆棧而不是簡單地使用segs。如何以編程方式嵌入導航控制器? – 2015-04-04 10:28:16

+1

我會更新我的答案。 – ABakerSmith 2015-04-04 10:36:32

相關問題