在Swift中,如何在没有故事板的情况下导航到另一个视图控制器?

时间:2015-08-04 18:41:51

标签: swift navigation push viewcontroller

在Swift中,如何在没有故事板的情况下导航到另一个视图控制器?

Swift的新手 - Objective C是一件苦差事

不使用任何故事板

手动创建选项卡式应用程序。

第一个标签是ReceiptsVC

  • 包含表格视图
  • 包含一个带有EDIT按钮的导航栏和一个“new”按钮,用于转到“NewVC”按钮调用函数newButtonAction2()

在viewdidload中

let navigationBar = UINavigationBar(frame: CGRectMake(0, 20, self.view.frame.size.width, 44)) // Offset by 20 pixels vertically to take the status bar into account
    navigationBar.backgroundColor = UIColor.whiteColor()
    navigationBar.delegate = self;

    // Create a navigation item with a title
    let navigationItem = UINavigationItem()
    navigationItem.title = "Receipts"

    // create the "new" button
    var rightButton = UIBarButtonItem(title: "New", style: UIBarButtonItemStyle.Plain, target: self, action: "newButtonAction2")

    // add table edit button to navigation bar
    navigationItem.leftBarButtonItem =  editButtonItem()
    // add new button to navigation bar
    navigationItem.rightBarButtonItem = rightButton

    // Assign the navigation item to the navigation bar
    navigationBar.items = [navigationItem]

    // Make the navigation bar a subview of the current view controller
    self.view.addSubview(navigationBar)

...

func  newButtonAction2() {

    println( "newButtonAction2 firing ")



    //let vc = NewVC(nibName: "NewVC", bundle: nil)
    //navigationController?.pushViewController(vc, animated: true)

    var vc = NewVC(nibName: "NewVC", bundle: nil)

    navigationController?.pushViewController(vc, animated: true)

    println( "newButtonAction2 firing  FINISHED")

}

输出

newButtonAction2开火 newButtonAction2射击结束 (NewVC永远不会出现)

我可以“呈现”视图控制器(模态)但我丢失了标签栏

app delegate

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


    let tabBarController = UITabBarController()
    //let myVC1 = TestViewController(nibName: "TestViewController", bundle: nil)
    let myVC1 = ReceiptsVC(nibName:  "ReceiptsVC", bundle: nil)
    let myVC2 = PieVC(nibName:  "DummyVC", bundle: nil)
    let myVC3 = PieVC(nibName:  "DummyVC", bundle: nil)
    let myVC4 = PieVC(nibName:  "DummyVC", bundle: nil)
    let myVC5 = PieVC(nibName:  "DummyVC", bundle: nil)
    let myVC6 = PieVC(nibName:  "DummyVC", bundle: nil)
    let controllers = [myVC1,myVC2,myVC3,myVC4,myVC5,myVC6]
    tabBarController.viewControllers = controllers
    window?.rootViewController = tabBarController
    let firstImage = UIImage(named: "Receipts25.png")
    let secondImage = UIImage(named: "Tab2.png")
    let Image3 = UIImage(named: "Tab3.png")
    let Image4 = UIImage(named: "Tab4.png")
    let Image5 = UIImage(named: "Tab5.png")
    let Image6 = UIImage(named: "Tab6.png")
    myVC1.tabBarItem = UITabBarItem(title: "Receipts", image: firstImage, tag: 1)
    myVC2.tabBarItem = UITabBarItem(title: "tab2", image: secondImage, tag:2)
    myVC3.tabBarItem = UITabBarItem(title: "tab3", image: Image3, tag:3)
    myVC4.tabBarItem = UITabBarItem(title: "tab4", image: Image4, tag:4)
    myVC5.tabBarItem = UITabBarItem(title: "tab5", image: Image5, tag:5)
    myVC6.tabBarItem = UITabBarItem(title: "tab6", image: Image6, tag:6)

  //   self.navigationController = UINavigationController(rootViewController: tabBarController)

    // self.navigationController = UINavigationController(rootViewController: ReceiptsVC())
   // self.window!.rootViewController = self.navigationController



    // Override point for customization after application launch.
    return true
}

2 个答案:

答案 0 :(得分:0)

我认为你需要在tabbarcontroller中为每个视图控制器提供导航控制器。

let myVC1 = UINavigationController(rootViewController: ReceiptsVC(nibName:  "ReceiptsVC", bundle: nil))

答案 1 :(得分:0)

为了将视图控制器推送到导航堆栈,您必须拥有导航堆栈。除非self是由导航控制器拥有的视图控制器,否则属性self.navigationController为nil。

此方法调用:

navigationController?.pushViewController(vc, animated: true)

如果navigationController为nil,则全部跳过。

如果你有一个标签栏控制器,并且标签栏中的每个标签都应该有它自己的导航堆栈,你需要设置这些标签指向那些包含根视图控制器的导航控制器。

因此,您应该修改所有nibfiles,以便放在标签栏中的每个视图控制器都包含在导航控制器中。

听起来你正在使用nibfiles而不是故事板?这比在代码中完全构建UI更容易。