V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
daiyuechuan
V2EX  ›  iDev

在一个闭包中无法向 UILabel 或者 UITextView 传递 text 值

  •  
  •   daiyuechuan ·
    CrazyNeil · 2015-03-18 09:31:43 +08:00 · 2188 次点击
    这是一个创建于 3332 天前的主题,其中的信息可能已经有所发展或是发生改变。
    有两个控件
    @IBOutlet weak var city: UILabel!
    @IBOutlet weak var showWeather: UITextView!

    现在的问题是,执行一个无返回值的函数,无法向这两个控件传递text值,而且更糟糕的是,如果向UITextView传值,直接造成程序崩溃。。。求指点


    //这个函数调用了一个天气API,返回JSON数据

    func returnWeatherInfo(){
    var request = HTTPTask()
    request.GET("http://m.weather.com.cn/data/101110101.html", parameters: nil, success: {(response: HTTPResponse) in

    if let data = response.responseObject as? NSData {
    let str = NSString(data: data, encoding: NSUTF8StringEncoding)
    println("response: \(str)") //prints the HTML of the page
    let json = JSON(data:data)
    var citytext = json["weatherinfo"]["city"].string
    let date = json["weatherinfo"]["date_y"].string
    println("\(citytext!),\(date!)")
    self.city.text = "\(citytext)" //这里开始向UILabel传值,不起作用
    self.showWeather.text. = "\(date)" //这里向UITextView里传值,造成程序崩溃
    }
    },failure: {(error: NSError, response: HTTPResponse?) in
    println("error: \(error)")

    })

    }
    4 条回复    2015-03-18 10:50:56 +08:00
    keithellis
        1
    keithellis  
       2015-03-18 09:41:20 +08:00
    检查更新 UI 操作是否在主线程进行
    ipconfiger
        2
    ipconfiger  
       2015-03-18 09:57:31 +08:00
    @keithellis 铁定是更新UI没有在主线程

    dispatch_async(dispatch_get_main_queue(),{
    if let data = response.responseObject as? NSData {
    ......
    });
    如此这般即可
    dorentus
        3
    dorentus  
       2015-03-18 10:28:48 +08:00
    不起作用应该是没在主线程调用,可以试试这样:

    dispatch_async(dispatch_get_main_queue()) {
    self.city.text = "\(citytext)"
    }

    崩溃的话就得看具体是什么错误了,常见的有例如 view 没加载,或者 outlet 没连接,导致 self.showWeather 还是 nil,然后就 EXC_BAD_ACCESS 闪退了
    daiyuechuan
        4
    daiyuechuan  
    OP
       2015-03-18 10:50:56 +08:00
    @dorentus @ipconfiger @keithellis

    非常感谢大家的回复,问题确实是对UI的操作没有跑在主线程,使用了 @ipconfiger 和 @dorentus 代码后均能解决问题。

    闪退的问题也找到了,是由于没有对应好optional类型引起的,感谢 @dorentus
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1994 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 01:25 · PVG 09:25 · LAX 18:25 · JFK 21:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.