Search code examples
swiftuiwebview

Access internal class variable


I am having a problem accessing the "youtubeURL" variable inside of "videoWebView". I am trying to access this variable inside of my didSet() function in "video". Inside of my didSet() I have put "videoWebView." This is where I would like to be able to access the "youtubeURL" variable. My final code I am looking for is something similar to this "videoWebView.youtubeURL = video?videoYoutubeURL". How do I gain access to the "youtubeURL" variable inside of "videoWebView"?

class VideoCell: BaseCell{

  var video: Video?{
    didSet{
      titleLabel.text = video?.title
      videoWebView.

    }
  }

  let videoWebView: UIWebView = {
    let videoWebView = UIWebView()
    let youtubeURL = "https://www.youtube.com/embed/T1-t9CbNxwg"

    videoWebView.loadHTMLString("<iframe width=\"336\" height=\"189\" src=\"\(youtubeURL)\" frameborder=\"0\" allowfullscreen></iframe>",baseURL: nil)

    videoWebView.contentMode = .ScaleAspectFill
    videoWebView.clipsToBounds = true
    return videoWebView
}()

let userProfileImageView: UIImageView = {
    let imageView = UIImageView()
    imageView.image = UIImage(named: "Gary Vee Profile Pic 1")
    imageView.layer.cornerRadius = 22
    imageView.layer.masksToBounds = true
    return imageView
}()

let separatorView: UIView = {
    let view = UIView()
    view.backgroundColor = UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1)

    return view
}()

let titleLabel: UILabel = {
    let label = UILabel()

    label.translatesAutoresizingMaskIntoConstraints = false
    label.text = "DailyVee 199"
    label.userInteractionEnabled = false
    return label
}()

let subtitleTextView: UITextView = {
    let textView = UITextView()
    textView.translatesAutoresizingMaskIntoConstraints = false
    textView.text = "When a street hustler make 130 million"
    textView.userInteractionEnabled = false
    textView.textContainerInset = UIEdgeInsetsMake(0,-4,0,0)
    textView.textColor = UIColor.darkGrayColor()
    return textView
}()

override func setupViews(){
    addSubview(videoWebView)
    addSubview(separatorView)
    addSubview(userProfileImageView)
    addSubview(titleLabel)
    addSubview(subtitleTextView)

    addContraintsWithFormat("H:|-16-[v0]-16-|", views: videoWebView)

    addContraintsWithFormat("H:|-16-[v0(44)]", views: userProfileImageView)



    //Vertical constraints
    addContraintsWithFormat("V:|-16-[v0]-8-[v1(44)]-16-[v2(1)]|", views: videoWebView, userProfileImageView, separatorView)

    addContraintsWithFormat("H:|[v0]|", views: separatorView)

    //top constraint
    addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Top, relatedBy: .Equal, toItem: videoWebView, attribute:.Bottom, multiplier: 1, constant: 8))
    //left constraint
    addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Left, relatedBy: .Equal, toItem: userProfileImageView, attribute:.Right, multiplier: 1, constant: 8))
    //right constraint
    addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Right, relatedBy: .Equal, toItem: videoWebView, attribute:.Right, multiplier: 1, constant: 0))
    //height constraint
    addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Height, relatedBy: .Equal, toItem: self, attribute:.Height, multiplier: 0, constant: 20))


    //top constraint
    addConstraint(NSLayoutConstraint(item: subtitleTextView, attribute: .Top, relatedBy: .Equal, toItem: titleLabel, attribute:.Bottom, multiplier: 1, constant: 4))
    //left constraint
    addConstraint(NSLayoutConstraint(item: subtitleTextView, attribute: .Left, relatedBy: .Equal, toItem: userProfileImageView, attribute:.Right, multiplier: 1, constant: 8))
    //right constraint
    addConstraint(NSLayoutConstraint(item: subtitleTextView, attribute: .Right, relatedBy: .Equal, toItem: videoWebView, attribute:.Right, multiplier: 1, constant: 0))
    //height constraint
    addConstraint(NSLayoutConstraint(item: subtitleTextView, attribute: .Height, relatedBy: .Equal, toItem: self, attribute:.Height, multiplier: 0, constant: 30))
    }
}

Solution

  • Try this. Let me know if it works

    func getHTMLString(url: String) -> String {
    
        return "<iframe width=\"336\" height=\"189\" src=\"\(url)\" frameborder=\"0\" allowfullscreen></iframe>"
    }
    
    class VideoCell: BaseCell{
    
        var video: Video?{
            didSet{
                if let vid = video {
                print(getHTMLString(vid.videoYoutubeURL ?? ""))
                videoWebView.loadHTMLString(getHTMLString(vid.videoYoutubeURL ?? ""),baseURL: nil)
                }
    
            }
        }