Ios 要从容器视图推送到另一个视图控制器吗
我有什么:带有一些“子/容器”视图的视图控制器(WhereViewController)。一个是集合视图(typeCollectionView)。其中ViewController嵌入到navigationController中: } 我正在尝试执行的操作:在typeCollectionView中:如果选择了一个项目(didSelectItemAt),我希望通过WhereViewController的navigationController推送到下一个viewController。我的typeCollectionView看起来像:Ios 要从容器视图推送到另一个视图控制器吗,ios,swift,uinavigationcontroller,uicollectionview,swift4,Ios,Swift,Uinavigationcontroller,Uicollectionview,Swift4,我有什么:带有一些“子/容器”视图的视图控制器(WhereViewController)。一个是集合视图(typeCollectionView)。其中ViewController嵌入到navigationController中: } 我正在尝试执行的操作:在typeCollectionView中:如果选择了一个项目(didSelectItemAt),我希望通过WhereViewController的navigationController推送到下一个viewController。我的typeC
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
}()
但是get:无法将类型“(NSObject)->()->WhereViewController”的值分配给类型“WhereViewController”
有人能帮我吗?或者是否可以从collectionView(containerView)与navigationController“对话”
我不使用故事板您应该使视图控制器符合集合委托方法,而不是集合视图本身 使
WhereViewController
符合UICollectionViewDataSource
、UICollectionViewDelegate
和UICollectionViewDelegateFlowLayout
:
class WhereViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout
在WhereViewController
的viewDidLoad
方法中,设置委托和数据源:
override func viewDidLoad() {
super.viewDidLoad()
typeCollectionView.dataSource = self
typeCollectionView.delegate = self
[...]
}
您应该使视图控制器符合集合委托方法,而不是集合视图本身 使
WhereViewController
符合UICollectionViewDataSource
、UICollectionViewDelegate
和UICollectionViewDelegateFlowLayout
:
class WhereViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout
在WhereViewController
的viewDidLoad
方法中,设置委托和数据源:
override func viewDidLoad() {
super.viewDidLoad()
typeCollectionView.dataSource = self
typeCollectionView.delegate = self
[...]
}
在这里,您可以使用委托将消息从
CollectionView
发送到
ViewController
简单声明协议
protocol CustomDelegate: class {
func collectionView(DidSelect : Int)
}
然后在WhereCollectionView
类中创建一个CustomDelegate
协议的实例
class WhereCollectionView: UICollectionView {
weak var customDelegate:CustomDelegate?
}
然后在didSelect
methods中启动委托方法
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let _del = customDelegate {
_del.collectionView(DidSelect : IndexPath.row)
}
}
在ViewController
中,您将在ViewController
中添加collectionview
设置delegate
到self
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()
}
在ViewController
func collectionView(DidSelect : Int) {
/// from there you can do your job
}
在这里,您可以使用委托将消息从
CollectionView
发送到
ViewController
简单声明协议
protocol CustomDelegate: class {
func collectionView(DidSelect : Int)
}
然后在WhereCollectionView
类中创建一个CustomDelegate
协议的实例
class WhereCollectionView: UICollectionView {
weak var customDelegate:CustomDelegate?
}
然后在didSelect
methods中启动委托方法
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let _del = customDelegate {
_del.collectionView(DidSelect : IndexPath.row)
}
}
在ViewController
中,您将在ViewController
中添加collectionview
设置delegate
到self
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()
}
在ViewController
func collectionView(DidSelect : Int) {
/// from there you can do your job
}
尝试:
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)
parentViewController?.present(,动画:true)
希望它有帮助尝试:
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)
parentViewController?.present(,动画:true)
希望对您有所帮助我尝试按照您的代码进行操作。此时:弱var委托:CustomDelegate?我收到一个错误,说:“类型为“CustomDelegate”的属性“delegate”无法重写类型为“UICollectionViewDelegate”的属性”;您能帮忙吗?您得到这个错误UICollectionView已经有委托变量了。只要更改CustomDelegate变量的名称,您的问题就会得到解决。我尝试按照您的代码进行操作。此时:弱var委托:CustomDelegate?我收到一个错误,说:“类型为“CustomDelegate”的属性“delegate”无法重写类型为“UICollectionViewDelegate”的属性”;您能帮忙吗?您得到这个错误UICollectionView已经有委托变量了。只要更改CustomDelegate变量的名称,您的问题就会得到解决。我已在以下位置实现了代码:“(typeCollectionView.collectionViewLayout as?UICollectionViewFlowLayout)?.delegate=self”我有一个错误:“UICollectionViewFlowLayout”类型的值没有成员“delegate”。我已在以下位置实现了代码:(typeCollectionView.collectionViewLayout作为?UICollectionViewFlowLayout)?.delegate=self“我有一个错误:“UICollectionViewFlowLayout类型的值没有成员‘delegate’”。