gpt4 book ai didi

ios - 来自 Web 服务的数据查找未在标签中正确显示

转载 作者:行者123 更新时间:2023-11-30 12:20:40 24 4
gpt4 key购买 nike

我有两个对 Web 服务的调用,我试图将这些调用显示在辅助 View 的标签中。

第一次查找或调用发生在主视图 Controller 加载期间,如下所示:

override func viewDidLoad() {
super.viewDidLoad()
getAddress()
}

func getAddress() {
let URL_GET_ADDR:String = "http://www.webserver.com/apps/getmailingaddr.php"
let requestURL = NSURL(string: URL_GET_ADDR)
let request = NSMutableURLRequest(url: requestURL! as URL)
request.httpMethod = "POST"
let task = URLSession.shared.dataTask(with: request as URLRequest){
data, response, error in
//exiting if there is some error
if error != nil{
print("error is \(error)")
return;
}
//parsing the response
do {
//converting resonse to NSDictionary
//var teamJSON: NSDictionary!
self.teamJSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

//getting the JSON array teams from the response
let address: NSArray = self.teamJSON["mailing"] as! NSArray

//looping through all the json objects in the array teams
for i in 0 ..< address.count{

//getting the data at each index
let addrName:String = (address[i] as! NSDictionary)["name"] as! String!
let addrAddr1:String = (address[i] as! NSDictionary) ["address1"] as! String!
let addrAddr2:String = (address[i] as! NSDictionary)["address2"] as! String!
let addrCity:String = (address[i] as! NSDictionary)["city"] as! String!
let addrState:String = (address[i] as! NSDictionary)["state"] as! String!
let addrZipcode:Int = (address[i] as! NSDictionary)["zipcode"] as! Int!
}

} catch {
print(error)
}
}
task.resume()
}

我使用以下内容将此数据传递到联系人 View Controller :

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let ContactsViewController = segue.destination as? ContactsViewController {
ContactsViewController.myAddress = teamJSON
}

到目前为止一切都很好。现在,在联系人 View Controller 中,我正在使用以下命令执行联系人查找:

override func viewDidLoad() {
super.viewDidLoad()
myLabel = ""
// Do any additional setup after loading the view, typically from a nib.
contactsLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
//created NSURL
let requestURL = NSURL(string: URL_GET_TEAMS)
//creating NSMutableURLRequest
let request = NSMutableURLRequest(url: requestURL! as URL)
//setting the method to post
request.httpMethod = "POST"
//creating a task to send the post request
let task = URLSession.shared.dataTask(with: request as URLRequest){
data, response, error in
//exiting if there is some error
if error != nil{
print("error is \(error)")
return;
}
//parsing the response
do {
//converting resonse to NSDictionary
var teamJSON: NSDictionary!
teamJSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
//getting the JSON array teams from the response
let teams: NSArray = teamJSON["contacts"] as! NSArray
//looping through all the json objects in the array teams
for i in 0 ..< teams.count{
//getting the data at each index
let teamId:String = (teams[i] as! NSDictionary)["title"] as! String!
let teamName:String = (teams[i] as! NSDictionary) ["name"] as! String!
//displaying the data
//print("id -> ", teamId)
//print("name -> ", teamName)
//print("===================")
//print("")
self.myLabel = self.myLabel + "<font size='5'><b>" + teamId + "</font>:</b> " + "<font size='5'>" + teamName + "</font><br /><br />"
}
} catch {
print(error)
}
DispatchQueue.main.async {
let attrStr = try! NSAttributedString(data: self.myLabel.data(using: String.Encoding.unicode,allowLossyConversion: true)!, options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
self.contactsLabel.attributedText = attrStr
self.contactsLabel.sizeToFit()
}
}
//executing the task
task.resume()

let attrStr = try! NSAttributedString(data: myLabel.data(using: String.Encoding.unicode,allowLossyConversion: true)!, options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
contactsLabel.attributedText = attrStr
contactsLabel.sizeToFit()
getAddress()
}

此处的 getAddress 函数调用获取传入的数据并将其添加到标签中:

func getAddress() {
//getting the JSON array teams from the response
let address: NSArray = myAddress["mailing"] as! NSArray
//looping through all the json objects in the array teams
for i in 0 ..< address.count{

//getting the data at each index
let addrName:String = (address[i] as! NSDictionary)["name"] as! String!
let addrAddr1:String = (address[i] as! NSDictionary) ["address1"] as! String!
let addrAddr2:String = (address[i] as! NSDictionary)["address2"] as! String!
let addrCity:String = (address[i] as! NSDictionary)["city"] as! String!
let addrState:String = (address[i] as! NSDictionary)["state"] as! String!
let addrZipcode:Int = (address[i] as! NSDictionary)["zipcode"] as! Int!

let myCityStateZip : String = addrCity + ", " + addrState + " " + String(addrZipcode)

let myName : String = addrName + "<br />" + addrAddr1 + "<br />" + addrAddr2

let myDescription : String = "*All Correspondence for the Board / Assoc.<br /> should be directed to the following address:<br /><br />"

self.myLabel = self.myLabel + "<b><font size='5'>" + myDescription + "</font></b>"
self.myLabel = self.myLabel + "<font size='4'>" + myName + "</font>"
self.myLabel = self.myLabel + "<font size='4'>" + myCityStateZip + "</font>"
self.myLabel = self.myLabel + "<br /><br />"

let attrStr = try! NSAttributedString(data: myLabel.data(using: String.Encoding.unicode,allowLossyConversion: true)!, options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
contactsLabel.attributedText = attrStr
contactsLabel.sizeToFit()
}
}

这是问题所在,第一次通过它可以完美地正确显示数据并按正确的顺序。如果您退出然后返回联系人页面,地址最终会出现在标签顶部,联系人如下。我似乎无法弄清楚为什么会发生这种情况。有任何想法吗?

最佳答案

我会将两个查找移动到前端(主视图 Controller 的 viewDidLoad 函数)并将它们作为值传递给第二个 Controller 。这样,您的第二个 Controller 只需关心格式化显示数据。

关于ios - 来自 Web 服务的数据查找未在标签中正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44830500/

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