Mo Code Mo Code - 7 months ago 51
Swift Question

Swift how to get webView to load faster

I've got a class VideoTableController that holds a tableview with a bunch of youtube videos in it.

When a video is selected it launches another view controller which has a web view in it that will play that specified youtube video.

I've tried putting the code to load the video in the prepareForSegue function of the videoTableController but the thing is that the VideoPlayer controller hasn't been instantiated yet so I can't modify the value of the video_view: UIWebView in the prepareForSegue function.

I was wondering if I do this videoPlayer() to instantiate it in the prepareForSegue function and then modify the value VideoPlayer.video_view.loadHTMLString() will that work or when the videoPlayer is segued to will another instance of it be instantiated?

Here is what I currently have in my videoPlayer class:

var video_player_url = ""

class VideoPlayer: UIViewController {

@IBOutlet weak var video_view: UIWebView!

override func viewDidLoad() {
self.video_view.loadHTMLString("<iframe width=\"\(video_view.frame.width)\" height=\"\(video_view.frame.height)\" src=\"https://www.youtube.com/embed/\(video_player_url)\" frameborder=\"0\" allowfullscreen></iframe>", baseURL: nil)
super.viewDidLoad()
// Do any additional setup after loading the view.
}


Here is my videoTableController class that calls that:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("video_player_segue", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let indexPath = self.tableView.indexPathForSelectedRow

video_player_url = video_urls[indexPath!.row]
}

Answer

In prepareForSegue, the class has been instantiated already but it may not have been loaded from the StoryBoard yet. This is how it is able to give you the destination controller inside prepareForSegue. The common technique is to set some other variable in the class, then in viewWillAppear in the destination controller, use that value to do whatever it is you are wanting to do.

class VideoPlayer : ViewController {
    var video_player_url: String?
    @IBOutlet weak var video_view: UIWebView!

    override func viewWillAppear(animated: Bool) {
        if let url = self.video_player_url {
            self.video_view.loadHTMLString("<iframe width=\"\(video_view.frame.width)\" height=\"\(video_view.frame.height)\" src=\"https://www.youtube.com/embed/\(video_player_url)\" frameborder=\"0\" allowfullscreen></iframe>", baseURL: nil)
        }

    }

}

And in your table view controller ...

class VideoTableController : UITableViewController {
   // stuff goes here

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let indexPath = self.tableView.indexPathForSelectedRow
    // verify that indexPath is not nil or your app will crash.
    let destinationController = segue.destinationViewController as! VideoPlayer
    destinationController.video_player_url = video_urls[indexPath!.row]
}
Comments