简体   繁体   中英

removing subview (uiview) in viewcontroller from collectionview

How do I remove a uiview that is added into a viewcontroller whenever the user clicks on one of the collectionview ? My collectionview is located inside of its own viewcontroller when the user clicks one of the collectionviewcell it will dismiss the controller (it's working) and then it will remove the contextTxt within the mainviewcontroller It did dismiss the view but it failed to remove the contextTxt and replacing it with a new view. In other words, the collectionviewcell did not trigger the @objc func inside the mainviewcontroller . Why won't it trigger?? and how do resolve this problem? Here is the code

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.
    }

}

Here is the collectionview code:

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")
    }
}

Because var mainVc=mainViewController() creates a new mainViewController which is not the existing mainViewController .
You can try getting the parent mainViewController by this way.

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

When presenting sideViewController, set the mainVc variable to self so that it is pointing to the actual mainVc object:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM