gpt4 book ai didi

swift - 在 Swift 中,如何在没有 Storyboard的情况下导航到另一个 View Controller ?

转载 作者:搜寻专家 更新时间:2023-11-01 05:40:02 24 4
gpt4 key购买 nike

在 Swift 中,如何在没有 Storyboard的情况下导航到另一个 View Controller ?

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

不使用任何 Storyboard

有一个手动创建的选项卡式应用程序。

第一个标签是 ReceiptsVC

  • 包含一个 TableView
  • 包括一个带有表格编辑按钮的导航栏和一个转到“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 从未出现)

我可以“呈现” View Controller (模态)但我丢失了标签栏

应用委托(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
}

最佳答案

我认为您需要为 tabbarcontroller 中的每个 View Controller 设置导航 Controller 。

喜欢

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

关于swift - 在 Swift 中,如何在没有 Storyboard的情况下导航到另一个 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31816851/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com