在 Swift 中,如何在没有故事板的情况下导航到另一个视图控制器?
In Swift how can I navigate to another view controller without storyboards?
在 Swift 中,我如何在没有故事板的情况下导航到另一个 viewcontroller?
Swift 的新手 - Objective C 是一件苦差事
不使用任何故事板
手动创建了一个选项卡式应用程序。
第一个标签是 ReceiptsVC
- 包含一个 table 视图
- 包括一个带有用于 table 的编辑按钮的导航栏和一个用于转到 "NewVC" 的 "new" 按钮,该按钮调用函数 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 从未出现)
我可以 "present" 视图控制器(模态)但我丢失了标签栏
应用委托
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
}
我认为您需要为 tabbarcontroller 中的每个视图控制器设置导航控制器。
喜欢
let myVC1 = UINavigationController(rootViewController: ReceiptsVC(nibName: "ReceiptsVC", bundle: nil))
为了将视图控制器推送到导航堆栈,您必须有一个导航堆栈。 属性 self.navigationController
为 nil 除非 self 是导航控制器拥有的视图控制器。
此方法调用:
navigationController?.pushViewController(vc, animated: true)
如果 navigationController 为 nil,则全部跳过。
如果您有一个选项卡栏控制器,并且选项卡栏中的每个选项卡都应该有自己的导航堆栈,您需要将这些选项卡设置为指向然后包含根视图控制器的导航控制器。
因此,您应该修改所有 nib 文件,以便您放入选项卡栏中的每个视图控制器都包含在导航控制器中。
听起来您使用的是 nibfile 而不是故事板?这肯定比完全用代码构建 UI 更容易。
在 Swift 中,我如何在没有故事板的情况下导航到另一个 viewcontroller?
Swift 的新手 - Objective C 是一件苦差事
不使用任何故事板
手动创建了一个选项卡式应用程序。
第一个标签是 ReceiptsVC
- 包含一个 table 视图
- 包括一个带有用于 table 的编辑按钮的导航栏和一个用于转到 "NewVC" 的 "new" 按钮,该按钮调用函数 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 从未出现)
我可以 "present" 视图控制器(模态)但我丢失了标签栏
应用委托
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
}
我认为您需要为 tabbarcontroller 中的每个视图控制器设置导航控制器。
喜欢
let myVC1 = UINavigationController(rootViewController: ReceiptsVC(nibName: "ReceiptsVC", bundle: nil))
为了将视图控制器推送到导航堆栈,您必须有一个导航堆栈。 属性 self.navigationController
为 nil 除非 self 是导航控制器拥有的视图控制器。
此方法调用:
navigationController?.pushViewController(vc, animated: true)
如果 navigationController 为 nil,则全部跳过。
如果您有一个选项卡栏控制器,并且选项卡栏中的每个选项卡都应该有自己的导航堆栈,您需要将这些选项卡设置为指向然后包含根视图控制器的导航控制器。
因此,您应该修改所有 nib 文件,以便您放入选项卡栏中的每个视图控制器都包含在导航控制器中。
听起来您使用的是 nibfile 而不是故事板?这肯定比完全用代码构建 UI 更容易。