想要从容器视图推送到另一个视图控制器

时间:2017-10-02 09:38:23

标签: ios swift uinavigationcontroller uicollectionview swift4

我有什么:带有一些“子/容器”视图的视图控制器(WhereViewController)。一个是集合视图(typeCollectionView)。 WhereViewController嵌入在navigationController中: 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“说话”?

我不使用故事板

3 个答案:

答案 0 :(得分:0)

您应该将视图控制器与集合委托方法和而不是集合视图本身相符合。

WhereViewController符合UICollectionViewDataSourceUICollectionViewDelegateUICollectionViewDelegateFlowLayout

class WhereViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout

WhereViewController的{​​{1}}方法中,设置代理和数据源:

viewDidLoad

答案 1 :(得分:0)

在这里,您可以使用委托从CollectionView发送消息 ViewController

简单声明一个协议

protocol CustomDelegate: class {
    func collectionView(DidSelect : Int)
}

之后在CustomDelegate Class

中创建WhereCollectionView协议的实例
class WhereCollectionView: UICollectionView {

weak var customDelegate:CustomDelegate?

}

并在didSelect方法中激活委托方法

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
   if let _del = customDelegate {
         _del.collectionView(DidSelect : IndexPath.row)
}

}

ViewController,您要collectionviewviewcontroller delegateself添加到override func viewDidLoad() { super.viewDidLoad() navigationItem.title = "Schaden melden" view.backgroundColor = UIColor.white typeCollectionView.customDelegate = self view.addSubview(progressContainerView) view.addSubview(questionLabel) view.addSubview(typeCollectionView) setupProgressContainerView() setupQuestionLabel() setupTypeCollectionView() }

Custom

同时在ViewController

中实施func collectionView(DidSelect : Int) { /// from there you can do your job } 委托方法
tensorflow/third_party/gpus/cuda_configure.bzl

答案 2 :(得分:0)

尝试:

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)

希望有所帮助

相关问题