在一个闭包中无法向 UILabel 或者 UITextView 传递 text 值 - V2EX
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 2784 次点击
    这是一个创建于 3940 天前的主题,其中的信息可能已经有所发展或是发生改变。
    有两个控件
    @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     Solana     1033 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 18:41 PVG 02:41 LAX 10:41 JFK 13:41
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86