0
我剛開始構建使用REST API的應用程序,我正在測試控制檯中的JSON響應。如何在不關閉和重新加載應用程序的情況下獲得新的JSON響應
我有2個UITextFields jobKeyword
& location
用戶輸入,然後單擊「搜索」按鈕時在控制檯中顯示JSON。
但是我不能在沒有關閉重新運行應用程序的情況下重新搜索。如何在不離開應用的情況下更新我的領域並獲得新的回覆?我在iOS開發中相當新。這裏是ViewController.swift:
import UIKit
import Alamofire
class ViewController: UIViewController {
let headers = ["Host":"data.usajobs.gov",
"User-Agent":"[email protected]",
"Authorization-Key":"********************"
]
@IBOutlet weak var jobKeywordTxt: UITextField!
@IBOutlet var locationTxt: UITextField!
@IBOutlet weak var locationState: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
}
@IBAction func searchBtn(sender: UIButton) {
let jobKeyword = jobKeywordTxt.text
let location = locationTxt.text
Alamofire.request(.GET, "https://data.usajobs.gov/api/Search", parameters:["LocationName":location!,"PositionTitle":jobKeyword!]).responseJSON { response in
debugPrint(response)
print(self.locationTxt.text)
}
}
}
當您點擊第二次搜索時會發生什麼?代碼看起來沒問題。 – smozgur
我不確定你是否在討論異步函數,在後臺請求和更新,或者如果它是獲取JSON的問題。 – UlyssesR
例如說:我在jobKeyword文本字段中輸入「software」,並在位置文本字段中輸入「Miami,Florida」。當我點擊搜索時,我在控制檯中獲得JSON響應。現在,如果我清除字段併爲每個字段輸入新值(例如,「護士」和「德克薩斯州達拉斯」),然後再次單擊搜索按鈕,則不會發生任何事情! –