ios - 想要从容器 View 推送到另一个 View Controller

标签 ios swift uinavigationcontroller uicollectionview swift4

我有什么:带有一些“子/容器” View 的 View Controller (WhereViewController)。一种是 Collection View (typeCollectionView)。 WhereViewController 嵌入在导航 Controller 中: Screenshot

class WhereViewController: UIViewController {

lazy var progressContainerView: ProgressBarView = {
    let containerView = ProgressBarView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))

    containerView.translatesAutoresizingMaskIntoConstraints = false

    return containerView
}()

let questionLabel: UILabel = {
    let label = UILabel()

    label.text = "question?"
    label.textAlignment = NSTextAlignment.center
    label.font = UIFont.init(name: "OpenSans-Italic", size: 14)
    label.textColor = UIColor(red:0.33, green:0.33, blue:0.33, alpha:1.0)
    label.translatesAutoresizingMaskIntoConstraints = false

    return label
}()

let typeCollectionView: WhereCollectionView = {
    let collectionView = WhereCollectionView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))

    collectionView.translatesAutoresizingMaskIntoConstraints = false

    return collectionView
}()

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.title = "Schaden melden"

    view.backgroundColor = UIColor.white


    view.addSubview(progressContainerView)
    view.addSubview(questionLabel)
    view.addSubview(typeCollectionView)

    setupProgressContainerView()
    setupQuestionLabel()
    setupTypeCollectionView()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

private func setupProgressContainerView() {
    progressContainerView.topAnchor.constraint(equalTo: view.topAnchor, constant: 60).isActive = true
    progressContainerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    progressContainerView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
    progressContainerView.heightAnchor.constraint(equalToConstant: 50).isActive = true
}

private func setupQuestionLabel() {
    questionLabel.topAnchor.constraint(equalTo: progressContainerView.bottomAnchor, constant: 22).isActive = true
    questionLabel.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    questionLabel.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
    questionLabel.heightAnchor.constraint(equalToConstant: 20).isActive = true
}

private func setupTypeCollectionView() {
    typeCollectionView.topAnchor.constraint(equalTo: questionLabel.bottomAnchor, constant: 20).isActive = true
    typeCollectionView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    typeCollectionView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
    typeCollectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}

我想要做什么: 在 typeCollectionView 中:如果选择了一个项目 (didSelectItemAt),我想通过 WhereViewController 的 navigationController 推送到下一个 viewController。我的 typeCollectionView 看起来像:

class WhereCollectionView: UICollectionView, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

var whereViewController: WhereViewController?

let label = ["x", "y", "z"]

override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
    let layout = UICollectionViewFlowLayout()
    super.init(frame: CGRect.zero, collectionViewLayout: layout)

    backgroundColor = UIColor.white

    delegate = self
    dataSource = self

    self.register(WhereCollectionViewCell.self, forCellWithReuseIdentifier: "WhereCollectionViewCell")
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return label.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WhereCollectionViewCell", for: indexPath) as! WhereCollectionViewCell

    cell.injureLabel.text = label[indexPath.row]

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: self.frame.width, height: 75)
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

}

我已经尝试将 whereViewController 赋予 typeCollectionView:

let typeCollectionView: WhereCollectionView = {
    let collectionView = WhereCollectionView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))

    collectionView.translatesAutoresizingMaskIntoConstraints = false
    collectionView.whereViewController = self 

    return tableView
}()

但是得到:无法将类型“(NSObject) -> () -> WhereViewController”的值分配给类型“WhereViewController?”

有人可以帮助我吗?还是不能与 collectionView (containerView) 中的 navigationController 进行“对话”?

我不使用 Storyboard

最佳答案

尝试:

1) 添加扩展:

extension UIView {

   var parentViewController: UIViewController? {
      var parentResponder: UIResponder? = self
      while parentResponder != nil {
         parentResponder = parentResponder!.next
         if parentResponder is UIViewController {
            return parentResponder as! UIViewController!
         }
      }

      return nil
   }
}

2) 现在你可以使用:

parentViewController?.present(<ControllerName>, animated: true)

希望对你有帮助

关于ios - 想要从容器 View 推送到另一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46523053/

相关文章:

ios - NSFetchedResultsController 不使用谓词@count 调用委托(delegate)

ios - Firebase 数据的服务器端过滤

ios - 带搜索栏跳转的导航 Controller

ios - 使用标签栏 Controller 和导航 Controller

ios - UIView segue UINavigationBar 闪烁

iphone - UINavigationController 与 UISplitViewController

ios - 这样的集成测试可能吗?

ios - 当我尝试从 Firebase 检索数据库信息时,为什么 SwiftUI 会显示错误?

ios - 在 swift 编程语言中使用隐式定义的常量类型有什么好处?

objective-c - 需要左值作为赋值左操作数