从collectionview中删除viewcontroller中的子视图(uiview)

时间:2018-07-20 02:11:34

标签: ios swift

每当用户单击uiview之一时,如何删除添加到viewcontroller中的collectionview?当用户单击collectionview之一时,我的viewcontroller位于自己的collectionviewcell内部,它将关闭控制器(正在工作),然后将其中的contextTxt移开mainviewcontroller确实关闭了视图,但是未能删除contextTxt并将其替换为新视图。换句话说,collectionviewcell不会触发@objc内部的mainviewcontroller函数。为什么不触发?以及如何解决这个问题?这是代码

class mainViewController: UIViewController {

    let navbar:navbarView = {
       let content = navbarView()
        return content
    }()

    let contentTxt:UITextView = {
       let content = UITextView()
        content.backgroundColor = UIColor.green
        return content
    }()

    let bottomBtn:UIButton = {
       let content = UIButton()
        content.backgroundColor = UIColor.red
        return content
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        navbar.translatesAutoresizingMaskIntoConstraints = false
        contentTxt.translatesAutoresizingMaskIntoConstraints = false
        bottomBtn.translatesAutoresizingMaskIntoConstraints = false

        view.addSubview(navbar)
        view.addSubview(contentTxt)
        contentTxt.addSubview(bottomBtn)

        navbar.topAnchor.constraint(
            equalTo: view.topAnchor, constant: 20).isActive = true
        navbar.centerXAnchor.constraint(
            equalTo: view.centerXAnchor).isActive = true
        navbar.widthAnchor.constraint(
            equalTo: view.widthAnchor).isActive = true
        navbar.heightAnchor.constraint(
            equalToConstant: 50).isActive = true

        contentTxt.topAnchor.constraint(
            equalTo: navbar.bottomAnchor, constant: 5).isActive = true
        contentTxt.centerXAnchor.constraint(
            equalTo: view.centerXAnchor).isActive = true
        contentTxt.widthAnchor.constraint(
            equalTo: view.widthAnchor).isActive = true
        contentTxt.bottomAnchor.constraint(
            equalTo: view.bottomAnchor, constant: 0).isActive = true

        bottomBtn.bottomAnchor.constraint(
            equalTo: contentTxt.bottomAnchor).isActive = true
        bottomBtn.centerXAnchor.constraint(
            equalTo: contentTxt.centerXAnchor).isActive = true
        bottomBtn.widthAnchor.constraint(
            equalToConstant: 40).isActive = true
        bottomBtn.heightAnchor.constraint(
            equalToConstant: 40).isActive = true
    }

    @objc func addConnections(){
        self.contentTxt.removeFromSuperview()
        let connect:connectView = {
            let content = connectView()
            content.backgroundColor = UIColor.red
            return content
        }()

        connect.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(connect)
        connect.topAnchor.constraint(
            equalTo: navbar.bottomAnchor, constant: 5).isActive = true
        connect.centerXAnchor.constraint(
            equalTo: view.centerXAnchor).isActive = true
        connect.widthAnchor.constraint(
            equalTo: view.widthAnchor).isActive = true
        connect.bottomAnchor.constraint(
            equalTo: view.bottomAnchor, constant: 0).isActive = true
    }
    @objc func sideController(){
        let next = self.storyboard?.instantiateViewController(
            withIdentifier: "sideViewController") as! sideViewController
        self.present(next, animated: true, completion: nil)
    }

    @objc func profileController(){
        let next = self.storyboard?.instantiateViewController(
            withIdentifier: "profileViewController") as! profileViewController
        self.present(next, animated: true, completion: nil)
    }

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

}

这是collectionview代码:

class sideCollectionView:UIView, UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
    var currentVc:sideViewController?
    var mainVc=mainViewController()
    let arrayLbl = ["connection","achievement","template","setting"]
    let arrayImg = ["connection","achievement","template","setting"]
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return arrayLbl.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! sideCollectionViewCell
        cell.titleImg.image = UIImage(named: "\(arrayImg[indexPath.row])")
        cell.titleLbl.text = arrayLbl[indexPath.row]
        return cell
    }

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

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(25, 25, 10, 25)
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.row == 0{
            print("connection")
            currentVc?.bye()
            mainVc.addConnections()
        }
        if indexPath.row == 1{
            print("achievement")
            currentVc?.bye()
        }
        if indexPath.row == 2{
            print("template")
            currentVc?.bye()
        }
        if indexPath.row == 3{
            print("setting")
            currentVc?.bye()
        }
    }

    lazy var collectionViews: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.backgroundColor = UIColor.clear
        cv.dataSource = self
        cv.delegate = self
        return cv
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

    func setupViews(){
        collectionViews.register(sideCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        collectionViews.translatesAutoresizingMaskIntoConstraints = false
        backgroundColor = UIColor.clear
        addSubview(collectionViews)
        collectionViews.topAnchor.constraint(equalTo: topAnchor, constant: 0).isActive = true
        collectionViews.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0).isActive = true
        collectionViews.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0).isActive = true
        collectionViews.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0).isActive = true
        collectionViews.widthAnchor.constraint(equalTo: widthAnchor, constant: 0).isActive = true
    }

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

2 个答案:

答案 0 :(得分:0)

因为var mainVc=mainViewController()创建了一个新的mainViewController,而不是现有的mainViewController
您可以尝试通过这种方式获取父mainViewController。

var mainVc: mainViewController? {
    return currentVc?.presentingViewController as? mainViewController
}

答案 1 :(得分:0)

在显示sideViewController时,请将mainVc变量设置为self,使其指向实际的mainVc对象:

@objc func sideController(){
    let next = self.storyboard?.instantiateViewController(withIdentifier: "sideViewController") as! sideViewController
    next.mainVc = self
    self.present(next, animated: true, completion: nil)
}