简体   繁体   中英

how to push ViewController from xib tableview Cell?

I have tableView within collectionView and i created using xib. I want to pushViewController when item is selected .I tried pushing the view controller in itemForRowAt method but it's not possible

class SecondTableView: UITableViewCell , UIcollectionViewDelegate ,UICollectionViewDataSource {


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

//        let vc = DataTableViewController()
//        vc.delegate = self
//        vc.pushNav()

        let memeVC = storyboard?.instantiateViewController(withIdentifier: "MemeViewController") as! MemeViewController
        memeVC.imagePassed = image
        navigationController?.pushViewController(memeVC, animated: true)
          print("item tapped\(indexPath)")
    }
}

errors Use of unresolved identifier 'storyboard'; did you mean 'UIStoryboard'?

Add segue as mentioned in the image and select show

在此处输入图片说明

Set segue identifier in your storyboard as mentioned in the image

Add below Protocol:

protocol CollectionViewCellDelegate: class {
    func userDidTap()
}

Add below property in your table view class where the collection view delegate returns.

weak var delegate: CollectionViewCellDelegate?

Update delegate method

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
              delegate.userDidTap()
              print("item tapped\(indexPath)")
        }
    }

confirm CollectionViewCellDelegate delegate to your first view controller & add this method:

func userDidTap() {
    performSegue(withIdentifier: "showMemeViewController", sender: nil)
}

Add prepare for segue in your first VC.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showMemeViewController" {
            let memeVC: MemeViewController = segue.destination as! MemeViewController
            memeVC.imagePassed = image
        }
    }

Please try this let me know if you faced any other issue. Thanks.

You're trying to access storyboard property in a tableview cell, which isn't there in the first place, it's a UIViewController property not a UITableViewCell property, hence the error, you'll have to delegate your selection to the viewController then let the controller do the segue normally.

You can start doing that via delegates as I mentioned above, for example if you're passing image to the controller you wanna segue to:

protocol ImageTableViewCellDelegate: class {
    // pass the the image or any variable you want in the func params
    func userDidTap(_ image: UIImage)
}

then you'd add it to the cell and confirm the controller to it

class SecondTableView {
  weak var delegate: ImageTableViewCellDelegate?
}

In your controller:

extension MyViewController: ImageTableViewCellDelegate {
    func userDidTap(_ image: UIImage){
        let memeVC = storyboard?.instantiateViewController(withIdentifier: "MemeViewController") as! MemeViewController
        memeVC.imagePassed = image
        navigationController?.pushViewController(memeVC, animated: true)
          print("item tapped\(indexPath)")
     }

}

this should work, let me know if you had any further problems

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