Ios 如何使用按钮从一个ViewController导航到另一个ViewController。Swift 3(无情节提要)
我想从名为“HomeController”的初始视图控制器导航到另一个名为“CalendarController”的视图控制器。我想通过使用一个名为“CalendarButton”的按钮来实现这一点。我也想这样做,不涉及故事板。谢谢 错误说明:不支持推送导航控制器Ios 如何使用按钮从一个ViewController导航到另一个ViewController。Swift 3(无情节提要),ios,objective-c,iphone,swift3,xcode8,Ios,Objective C,Iphone,Swift3,Xcode8,我想从名为“HomeController”的初始视图控制器导航到另一个名为“CalendarController”的视图控制器。我想通过使用一个名为“CalendarButton”的按钮来实现这一点。我也想这样做,不涉及故事板。谢谢 错误说明:不支持推送导航控制器 import UIKit class HomeController: UICollectionViewController,UICollectionViewDelegateFlowLayout { private let c
import UIKit
class HomeController: UICollectionViewController,UICollectionViewDelegateFlowLayout {
private let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = UIColor.rgb(31, green: 194, blue: 31)
collectionView!.isScrollEnabled = false
collectionView? .register(MenuCell.self, forCellWithReuseIdentifier: cellId)
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for:indexPath) as! MenuCell
cell.calendarButton.addTarget(self, action: #selector(calendar), for: .touchUpInside)
return cell
}
func calendar() {
let objVC: CalendarController! = CalendarController()
let aObjNavi = UINavigationController(rootViewController: objVC!)
self.navigationController?.pushViewController(aObjNavi, animated: true)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) ->Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}
//单独的swift文件
class BaseCell: UICollectionViewCell{
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
func setupViews() {
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class MenuCell: BaseCell{
lazy var calendarButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Calender", for: .normal)
button.setTitleColor(UIColor(red: 0, green: 0, blue: 0, alpha: 1), for: .normal)
button.backgroundColor = .white
button.titleLabel!.font = UIFont.systemFont(ofSize: 20)
button.layer.cornerRadius = 15
return button
}()
func calendar() {
let newViewController = CalendarController()
self.navigationController?.pushViewController(newViewController, animated: true)
}
正如我看到的,您的代码没有嵌入导航控制器。 “推”在堆栈中工作。因此,您需要在代码中以编程方式嵌入导航控制器 此链接可以更好地帮助您:
单元格没有导航控制器。只有视图控制器具有导航控制器 如果您想访问
func calendar() {
let newViewController = CalendarController()
self.navigationController?.pushViewController(newViewController, animated: true)
}
然后在HomeController中更改如下代码
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for:indexPath) as! MenuCell
cell.calendarButton.addTarget(self, action: #selector(calendar), for: .touchUpInside)
return cell
}
func calendar() {
let newViewController = CalendarController()
self.navigationController?.pushViewController(newViewController, animated: true)
}
并确保您的HomeController已嵌入到导航控制器中
或者您可以使用委托访问func calendar(){…}应用程序代理不是视图控制器显示代码,如:class HomeController…希望进行此编辑helps@ErikLopez您的HomeController是否在导航堆栈中??我对HomeController进行了更改,并且知道我收到了一个错误,即不支持推送导航控制器。下一步我需要做什么?当你实例化你的HomeController时,它必须嵌入到NavigationViewController中。我更新了我的代码,现在我收到一个“不支持推导航控制器”错误。请帮帮我,我是新手