以编程方式弹出导航堆栈

时间:2016-08-29 21:46:50

标签: ios swift uinavigationcontroller stack uitabbarcontroller

我在下面定义了UIPageViewController

class MainPageViewController: UIPageViewController {


var view1 : UIViewController = UIViewController()
var view2 : UIViewController = UIViewController()
var view3 : UIViewController = UIViewController()

var current_index = 0

override func viewDidLoad() {
    super.viewDidLoad()

    dataSource = self
    delegate = self

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let feed = storyboard.instantiateViewControllerWithIdentifier("view1")

    view1 = UINavigationController(rootViewController: feed)

    let requests = storyboard.instantiateViewControllerWithIdentifier("view2_1")

    let view2_1 = UINavigationController(rootViewController: requests)
    let chat = storyboard.instantiateViewControllerWithIdentifier("view2_2")
    let view2_2 = UINavigationController(rootViewController: chat)
    let matches = storyboard.instantiateViewControllerWithIdentifier("view2_3")
    let view2_3 = UINavigationController(rootViewController: matches)

    let view2 = UITabBarController()
    view2.viewControllers = [view2_1 , view2_2 , view2_3]

    let item1 = UITabBarItem(title: "Requests",image: UIImage(named : "request_filled_maintab") , tag: 0)
    let item2 = UITabBarItem(title: "Chats", image: UIImage(named : "chat_filled_maintab"), tag: 1)
    let item3 = UITabBarItem(title: "Matches", image: UIImage(named : "matches_filled_tab"), tag: 2)

    view2_1.tabBarItem = item1
    view2_2.tabBarItem = item2
    view2_3.tabBarItem = item3



    let story = storyboard.instantiateViewControllerWithIdentifier("view3")
    view3 = UINavigationController(rootViewController: story)

    view1.view.tag = 0
    view3.view.tag = 1
    view2.view.tag = 2

}

基本上有3个视图,view1 view2view3。现在我正在添加推送通知处理,我需要做的是如果用户在view1view3(两个导航控制器)我需要将导航堆栈弹出到根视图控制器然后继续view2

所以基本上我的布局就像这个UIPageViewController - >

UINavigationController(view1)

UINavigationController(view3)

UITabBarController(view2)

我正在尝试这种方法:

if var topController = UIApplication.sharedApplication().keyWindow?.rootViewController {
 while let presentedViewController = topController.presentedViewController {
            topController = presentedViewController
  }
    topController.navigationController?.popToRootViewControllerAnimated(true)
}
  self.goToNextPage()
  self.goToNextPage()
  self.view2!.selectedIndex = 1

如果我在rootViewController的{​​{1}},此代码有效,但在任何其他情况下,它似乎没有做任何事情。我不确定为什么。

navigationController是一种以编程方式将pageViewController移动到下一页的方法)

0 个答案:

没有答案