大多数时候HTML不会第一次加载

时间:2015-08-13 07:04:08

标签: ios iphone xcode swift

我有这个用Swift制作的应用程序,因为我很有趣,因为我是初学者。基本上,它可以输入艺术家姓名和歌曲名称,并根据您输入的艺术家和歌曲名称从AZLyrics.com获取歌词。出于某种原因,大多数时候,当我第一次尝试加载歌词时,我打开应用程序时它没有加载(它加载第2,第3,第4等时间)。这是我的代码可以有人请告诉我如何解决这个问题?

第一个视图控制器

import UIKit

var lyricsWithQuotes = ""
var urlError = false

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet var songName: UITextField!
@IBOutlet var artistName: UITextField!

@IBAction func search(sender: AnyObject) {
    var url = NSURL(string: "http://www.azlyrics.com/lyrics/" + artistName.text.stringByReplacingOccurrencesOfString(" ", withString: "").stringByReplacingOccurrencesOfString("'", withString: "").stringByReplacingOccurrencesOfString(",", withString: "").stringByReplacingOccurrencesOfString(".", withString: "").lowercaseString + "/" + songName.text.stringByReplacingOccurrencesOfString(" ", withString: "").stringByReplacingOccurrencesOfString("'", withString: "").stringByReplacingOccurrencesOfString(",", withString: "").stringByReplacingOccurrencesOfString(".", withString: "").lowercaseString  + ".html")

    if url != nil {

        let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in

            if error == nil {

                var urlContent = NSString(data: data, encoding: NSUTF8StringEncoding) as NSString!

                var urlContentArray = urlContent.componentsSeparatedByString("<!-- Usage of azlyrics.com content by any third-party lyrics provider is prohibited by our licensing agreement. Sorry about that. -->")

                if urlContent.containsString("It's a place where all searches end!") {

                    urlError = true

                } else {

                    var lyricsArray = urlContentArray[1].componentsSeparatedByString("<br><br>")

                    var lyrics = lyricsArray[0] as! String

                    var lyricsWithoutBR = lyrics.stringByReplacingOccurrencesOfString("<br>", withString: "")

                    var lyricsWithoutSlashDiv = lyricsWithoutBR.stringByReplacingOccurrencesOfString("</div>", withString: "")

                    var lyricsWithoutI = lyricsWithoutSlashDiv.stringByReplacingOccurrencesOfString("<i>", withString: "")

                    var lyricsWithoutSlashI = lyricsWithoutI.stringByReplacingOccurrencesOfString("</i>", withString: "")

                    lyricsWithQuotes = lyricsWithoutSlashI.stringByReplacingOccurrencesOfString("&quot;", withString: "\"")

                }

            } else {

                urlError = true

            }

        })

        task.resume()

    } else {

        urlError = true

    }


}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    view.endEditing(true)
}

func textFieldShouldReturn(textField: UITextField) -> Bool {

    textField.resignFirstResponder()

    return true
}

override func viewDidLoad() {
    super.viewDidLoad()

    artistName.delegate = self

    songName.delegate = self

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

第二视图控制器

import UIKit

var lyricsComplete = ""

class ViewController2: UIViewController {

@IBOutlet var lyricsDisplay: UITextView!

override func viewDidAppear(animated: Bool) {

    if urlError == true {

        urlError = false
        lyricsDisplay.text = "Couldn't find that song!"

    } else {

        lyricsComplete = lyricsWithQuotes
        lyricsDisplay.text = lyricsComplete

    }
}

override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

1 个答案:

答案 0 :(得分:0)

NSURLSession完成后 - 基本上就是这一行:

lyricsWithQuotes = lyricsWithoutSlashI.stringByReplacingOccurrencesOfString("&quot;", withString: "\"")

您有两种选择:

选项一 - 以编程方式启动视图:

let storyboard = UIStoryboard(name: "STORYBOARD NAME HERE", bundle: nil)
let newVC = storyboard.instantiateViewControllerWithIdentifier("VIEW CONTROLLER IDENTIFIER HERE") as! ViewController2
newVC.lyricsComplete = lyricsWithQuotes

如果您有导航控制器:

self.navigationController?.pushViewController(newVC, animated: true)

如果您没有导航控制器:

self.showViewController(newVC, sender: self)

选项2 - 触发Segue:

performSegueWithIdentifier("SEGUE NAME HERE", sender: nil)

然后拦截Segue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.

    if(segue.identifier == "SEGUE NAME HERE"){
        let newVC = segue.destinationViewController as! ViewController2
        newVC.lyricsComplete = lyricsWithQuotes
    }
}

如果您使用选项2,请确保Segue位于View Controllers之间且未连接到您的按钮:

像这样:

Segue Correct

不喜欢这样:

Segue Incorrect