我的程序完全正常,但是如果我点击“Enter”按钮,它将不会再次更新。它是我第一次点击它,但不是第二次。
下面是代码:
import UIKit
class ViewController: UIViewController, NSURLConnectionDelegate {
lazy var data = NSMutableData()
@IBOutlet weak var searchField: UITextField!
@IBOutlet weak var name: UILabel!
@IBOutlet weak var summonerLevel: UILabel!
@IBOutlet weak var profileIconId: UILabel!
@IBAction func enterButton(sender: AnyObject) {
startConnection()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func startConnection(){
println("start connection began")
let urlPath: String = "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/" + searchField.text + "?api_key=dd5bf142-df4a-4eed-be7f-8f6f908dcfe6"
var url: NSURL = NSURL(string: urlPath)!
var request: NSURLRequest = NSURLRequest(URL: url)
var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)!
connection.start()
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!){
self.data.appendData(data)
}
func buttonAction(sender: UIButton!){
startConnection()
}
func connectionDidFinishLoading(connection: NSURLConnection) {
var err: NSError?
if let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as? NSDictionary,
let include = jsonResult.objectForKey(searchField.text) as? NSDictionary {
//where the program stops running after I click enter the second time (it doesn't literally 'stop' running, it just doesn't execute the rest of this code, keep in mind its the second time I click enter with a new username in the textfield value)
if let name1 = include[ "name" ] as? String {
name.text = name1
println(name1)
}
if let summLevel = include[ "summonerLevel" ] as? NSNumber {
summonerLevel.text = "\(summLevel.integerValue)"
println(summLevel)
}
if let profIconId = include[ "profileIconId" ] as? NSNumber {
profileIconId.text = "\(profIconId.integerValue)"
println(profIconId)
}
}
}
继承我正在解析的内容(我不认为它有任何用处,但以防万一):
{"soon2challenger":{"id":43993167,"name":"soon2challenger","profileIconId":844,"summonerLevel":30,"revisionDate":1435549418000}}
我希望程序执行的操作是,当我为文本字段键入不同的值并单击“Enter”时,我希望它使用新信息进行更新。我已经尝试将let值更改为vars,但这似乎并没有使它工作。
从我第一次输入值并在iOS模拟器中单击“Enter”时看到它的样子:
下面我第二次输入一个新值然后点击“Enter”看起来是什么样子,没有任何变化/发生:
答案 0 :(得分:1)
我认为有两个问题:
一个问题是,您可以在完成
之前单击按钮两次另一个是你总是积累可变数据而不是清空它
我在代码中添加了一些可能有用的注释。
import UIKit
class ViewController: UIViewController, NSURLConnectionDelegate {
lazy var data = NSMutableData()
@IBOutlet weak var searchField: UITextField!
@IBOutlet weak var name: UILabel!
@IBOutlet weak var summonerLevel: UILabel!
@IBOutlet weak var profileIconId: UILabel!
//Create a new outlet to the Button
@IBOutlet weak var enterButton: UIButton!
@IBAction func enterButton(sender: AnyObject) {
//disable the button until the request finishes
enterButton.enable = false
//Clear the old data from data
data.setData(NSData())
startConnection()
}
func startConnection(){
println("start connection began")
let urlPath: String = "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/" + searchField.text + "?api_key=dd5bf142-df4a-4eed-be7f-8f6f908dcfe6"
var url: NSURL = NSURL(string: urlPath)!
var request: NSURLRequest = NSURLRequest(URL: url)
var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)!
connection.start()
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!){
self.data.appendData(data)
}
//You don't need this you already have an outlet
func buttonAction(sender: UIButton!){
startConnection()
}
func connectionDidFinishLoading(connection: NSURLConnection) {
var err: NSError?
if let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as? NSDictionary,
let include = jsonResult.objectForKey(searchField.text) as? NSDictionary {
//where the program stops running after I click enter the second time (it doesn't literally 'stop' running, it just doesn't execute the rest of this code, keep in mind its the second time I click enter with a new username in the textfield value)
if let name1 = include[ "name" ] as? String {
name.text = name1
println(name1)
}
if let summLevel = include[ "summonerLevel" ] as? NSNumber {
summonerLevel.text = "\(summLevel.integerValue)"
println(summLevel)
}
if let profIconId = include[ "profileIconId" ] as? NSNumber {
profileIconId.text = "\(profIconId.integerValue)"
println(profIconId)
}
//re-enable the button for new requests
enterButton.enable = true
}
}