How to refer UICollectionView from UICollectionViewCell? -
can refer uicollectionview uicollectionviewcell? want refer viewcontroller uicollectionviewcell.
following codes in customcollectionviewcell class. want archive tweet support in class.
@ibaction func tweetbtntapped(_ sender: any) { let cvc = slcomposeviewcontroller(forservicetype: slservicetypetwitter) if let c = cvc { c.setinitialtext("test tweet ios app") ### how can refer viewcontroller?? viewcontroller = ??????? if let vc = viewcontroller { vc.present(c, animated: true, completion: nil) } } }
please provide code sample or trying achieve. understand want action in view controller when perform action on cell. correct?
so per comments, custom collection view cell :
class customecell: uicollectionviewcell {
@ibaction func tweetbuttonpressed() { let cvc = slcomposeviewcontroller(forservicetype:slservicetypetwitter) if let c = cvc { c.setinitialtext("test tweet ios app") self.parentviewcontroller?.presentviewcontroller(c, animated: true, completion: nil) } }
}
create extension.swift file :
import uikit
extension uiview {
var parentviewcontroller: uiviewcontroller? { var parentresponder: uiresponder? = self while parentresponder != nil { parentresponder = parentresponder!.nextresponder() if let viewcontroller = parentresponder as? uiviewcontroller { return viewcontroller } } return nil }
}
this working me. best!
Comments
Post a Comment