【问题标题】:How to navigate from one ViewController to another using a button. Swift 3 (No StoryBoard)如何使用按钮从一个 ViewController 导航到另一个。 Swift 3(无故事板)
【发布时间】:2018-01-16 17:52:37
【问题描述】:

我想从名为“HomeController”的初始 ViewController 导航到另一个名为“CalendarController”的视图控制器。我想通过使用一个名为“CalendarButton”的按钮来做到这一点。我也想在不参考情节提要的情况下这样做。谢谢

错误提示:不支持推送导航控制器

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)
}

【问题讨论】:

  • 应用委托不是视图控制器
  • 显示代码如:class HomeController...
  • 希望这次编辑对您有所帮助
  • @ErikLopez 是导航堆栈中的 HomeController 吗??

标签: ios objective-c iphone swift3 xcode8


【解决方案1】:

Cell 没有导航控制器。只有视图控制器有导航控制器。

如果你想访问

 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 嵌入在 NavigationController

或者您可以使用委托访问 func Calender(){...}

【讨论】:

  • 我对 HomeController 进行了更改,并且知道我收到错误消息,提示不支持推送导航控制器。接下来我需要做什么?
  • 当您实例化您的 HomeController 时,它必须嵌入到 NavigationViewController 中。
【解决方案2】:

我看到你的代码你没有嵌入导航控制器。 Push 在堆栈中工作。因此,您需要在代码中以编程方式嵌入导航控制器。

此链接可以更好地帮助您: Embedding navigation controller

【讨论】:

  • 我更新了我的代码,现在我收到“不支持推送导航控制器”错误。请帮助我是新手
猜你喜欢
  • 2020-05-28
  • 2012-02-13
  • 1970-01-01
  • 2020-11-16
  • 2017-01-04
  • 2017-02-22
  • 2013-09-17
  • 1970-01-01
  • 2016-05-04
相关资源
最近更新 更多