Ios 项目赢得';重新单击按钮后更新
我的程序运行完全正常,只是如果我重新点击“回车”按钮,它不会再次更新。我第一次单击它时,它就工作了,但第二次不工作 代码如下:Ios 项目赢得';重新单击按钮后更新,ios,json,swift,Ios,Json,Swift,我的程序运行完全正常,只是如果我重新点击“回车”按钮,它不会再次更新。我第一次单击它时,它就工作了,但第二次不工作 代码如下: import UIKit class ViewController: UIViewController, NSURLConnectionDelegate { lazy var data = NSMutableData() @IBOutlet weak var searchField: UITextField! @IBOutlet weak var name: U
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)
}
}
}
以下是我正在解析的内容(我认为它没有任何用处,只是以防万一):
我想让程序做的是,当我为textfield键入不同的值并单击“Enter”时,我想让它用新信息更新。我曾尝试将let值更改为vars,但这似乎不起作用
这是我第一次在iOS模拟器中输入值并单击“回车”时的样子:
这是我第二次输入新值并单击“回车”时的外观,实际上没有任何变化/发生:
我认为有两个问题: 一个问题是,您可以在按钮完成之前单击两次 另一个是,您总是在累积变量数据,而不是清空它 我在代码中添加了一些可能有用的注释
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
}
}
好消息!很高兴我能帮上忙,祝你顺利完成你的应用程序!:)
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
}
}