[英]Unable to update UILabel using dispatch_async method in swift iOS
I am setting json string value to UILabel using dispatch_async method. 我正在使用dispatch_async方法将json字符串值设置为UILabel。 But Its unable to show updated value , but when I am printing label text, its showing updated value. 但是它无法显示更新的值,但是当我打印标签文本时,其显示更新的值。 here is my code- 这是我的代码-
override func viewDidLoad() {
super.viewDidLoad()
get_detail_postmethod(url, params: params)
}
func get_detail_postmethod(url: String, params: String) {
let request = NSMutableURLRequest(URL: NSURL(string: url)!)
request.HTTPMethod = "POST"
let postString = params
print("param=\(params)")
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard error == nil && data != nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
do{
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
print(json)
if let success = json["success"] as? Int
{
if((success) == 1){
self.extract_json(data!)
}
else{
}
}
}catch {
print("Error with Json: \(error)")
}
}
task.resume()
}
and below code for extract json data and set String value to UILabel- 及以下代码以提取json数据并将String值设置为UILabel-
func extract_json(jsonData:NSData)
{
let json: AnyObject?
do {
json = try NSJSONSerialization.JSONObjectWithData(jsonData, options: [])
print("full data \(json!)")
} catch {
json = nil
return
}
let user_object = json!["opportunity_master"] as! NSArray
let data_block = user_object[0] as? NSDictionary
self.opp_title = (data_block!["opportunity_title"] as? String)!
self.event_sdate = (data_block!["event_start_date"] as? String)!
dispatch_async(dispatch_get_main_queue(), {
print(self.opp_title)
self.Event_title.text? = self.opp_title
self.Txt_event_date.text = self.event_sdate
self.Event_title.setNeedsDisplay()
print("event title label \(self.Event_title.text)")
})
}
I am not able to update values. 我无法更新值。 Please suggest is there any other way to implement update UiLabel text. 请建议还有其他实现更新UiLabel文本的方法。
This is solved by calling Viewcontroller like this- 这可以通过这样调用Viewcontroller来解决-
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let Opprt_Controller = storyboard.instantiateViewControllerWithIdentifier("Opprtunity_Event") as! Opportunity_Event
self.navigationController!.pushViewController(Opprt_Controller, animated: true)
Try the below code to update your Label : 请尝试以下代码来更新您的标签:
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
print(self.opp_title)
self.Event_title.text? = self.opp_title
self.Txt_event_date.text = self.event_sdate
self.Event_title.setNeedsDisplay()
print("event title label \(self.Event_title.text)")
})
在viewDidAppear中而不是viewDidLoad中调用get_detail_postmethod(url,params:params)方法。
There is a syntax error in your dispatch_async
method. 您的dispatch_async
方法中存在语法错误。 Update code block with following. 使用以下代码更新代码块。
dispatch_async(dispatch_get_main_queue()) {
print(self.opp_title)
self.Event_title.text? = self.opp_title
self.Txt_event_date.text = self.event_sdate
self.Event_title.setNeedsDisplay()
print("event title label \(self.Event_title.text)")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.