gpt4 book ai didi

ios - 重新单击按钮后程序不会更新

转载 作者:行者123 更新时间:2023-11-28 07:04:28 25 4
gpt4 key购买 nike

我的程序运行良好,除了如果我重新单击“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”时的样子:

This is what it looks like the first time

这是我第二次输入新值并单击“Enter”时的样子,实际上没有任何变化/发生:

This is what it looks like the second time

最佳答案

我认为有两个问题:

一个问题是你可以在按钮完成前点击两次

另一个是你一直在积累变量数据而不是清空

我在代码中添加了一些可能有帮助的注释。

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

关于ios - 重新单击按钮后程序不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31151901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com