当我单击collectionviewcell时,我很难解散viewController。所以我的collectionview放在uiview内,然后显示在viewcontroller中。每当用户单击collectionViewCell时,我都希望视图触发放置在viewcontroller内的bye()函数。我添加print(“ bye”)只是为了查看它是否有效,它确实会打印单词,但不会执行Blob 0 average value: true=0.75, estimated=0.75
Blob 1 average value: true=0.63, estimated=0.63
Blob 2 average value: true=0.50, estimated=0.49
来关闭uiview和collectionview的viewcontroller。为什么不解雇控制器?还有另一种我可以做同样的事情吗?这是代码:
视图控制器
dismiss(animated: true, completion: nil)
这是collectionView代码和uiview
class sideViewController: UIViewController {
let dismissBtn:UIButton = {
let content = UIButton()
content.backgroundColor = .green
content.addTarget(self, action: #selector(bye), for: .touchUpInside)
return content
}()
let sideTableViews: sideCollectionView = {
let content = sideCollectionView()
return content
}()
override func viewDidLoad() {
super.viewDidLoad()
dismissBtn.translatesAutoresizingMaskIntoConstraints = false
sideTableViews.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(dismissBtn)
view.addSubview(sideTableViews)
dismissBtn.topAnchor.constraint(equalTo: view.topAnchor, constant: 20).isActive = true
dismissBtn.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10).isActive = true
dismissBtn.widthAnchor.constraint(equalToConstant:40).isActive = true
dismissBtn.heightAnchor.constraint(equalToConstant: 40).isActive = true
sideTableViews.topAnchor.constraint(equalTo: dismissBtn.bottomAnchor, constant: 30).isActive = true
sideTableViews.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
sideTableViews.widthAnchor.constraint(equalTo: view.widthAnchor, constant: 0).isActive = true
sideTableViews.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
}
@objc func membershipController(){
let next = self.storyboard?.instantiateViewController(withIdentifier: "membershipViewController") as! membershipViewController
self.present(next, animated: true, completion: nil)
}
@objc func bye(){
print("bye")
dismiss(animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
因为这个
let side = sideViewController()
是除显示的实例之外的另一个实例,因此请将此var添加到视图中
class sideCollectionView:UIView {
var currentVc:sideViewController?
}
//
在创建变量时将其分配
lazy var sideTableViews: sideCollectionView = {
let content = sideCollectionView()
content.currentVc = self
return content
}()
//
内部
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.row == 0{
connectController()
print("connection")
currentVc?.bye()
}
}