访问内部类变量

时间:2017-04-07 00:32:04

标签: swift uiwebview

我在访问" youtubeURL"时遇到问题。 " videoWebView"内的变量。我试图在"视频"中的didSet()函数内部访问此变量。在我的didSet()里面我放了" videoWebView。"这是我希望能够访问" youtubeURL"变量。我正在寻找的最终代码类似于这个" videoWebView.youtubeURL = video?videoYoutubeURL"。如何访问" youtubeURL" " 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))
    }
}

1 个答案:

答案 0 :(得分:1)

试试这个。让我知道它是否有效

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

        }
    }