[問題] swift delegate傳值後顯示在label

作者: whitefox0102 (一個人踢球的小正)   2018-01-08 20:38:44
我在一個viewcontroller裡面分成上下兩塊,
上面是一個UIView
下面是一個container view並且有另外用一個ViewControllerB去控制
我在要傳值得controller加入了以下代碼
protocol sendItemPriceDelegate {
func sendPrice(_ price:String)
}
並在class 下面宣告
var delegate:sendItemPriceDelegate?
然後在API回來的地方加入
self.delegate = ViewControllerB() as sendItemPriceDelegate
delegate?.sendPrice(price)
回到controllerB繼承delegate並實作方法
func sendPrice(_ price: String) {
print(price)
itemPriceLabel!.text = price <===無法顯示出來
}
在印出price的時候是有值的
值是有傳過來的
但是要將它放在Label顯示出來卻一直顯示不出來
請大大們解惑
感謝~~
作者: gundam00 (傻那駕駛中)   2018-01-08 20:45:00
DispatchQueue.main.async{ table.text = value }試試看
作者: Sexyfather (Sexyfather)   2018-01-08 22:41:00
UI 更新應該是要放在main thread 沒錯
作者: cryinglove (毓™)   2018-01-09 09:40:00
你指定delegate時,是指給另一個你初始化出來的 viewControllerB
作者: whitefox0102 (一個人踢球的小正)   2018-01-09 11:38:00
一樓大大~照做了可是沒用耶Orzcryinglove大~那我應該如何做才對呢?
作者: RINPE (RIN)   2018-01-09 12:51:00
這行確定有進去嗎? itemPriceLabel!.text = price 設中斷看一下
作者: whitefox0102 (一個人踢球的小正)   2018-01-09 13:13:00
有進去但是label的值都沒有改變
作者: booray (波波)   2018-01-09 13:31:00
看起來你 ViewControllerB() 這裏弄出來另一個 viewcontrollerb 物件而不是顯示在螢幕上那個要不要考慮改用 notification 實作?
作者: whitefox0102 (一個人踢球的小正)   2018-01-09 14:31:00
剛剛改用了notifacation了~可是這中間會頓一下點擊按鈕傳送通知到改變Label大約要4秒甩笨了...自己多調用了別的func,使用notifacation後就成功實現了~感謝樓上諸位大大的幫忙^^

Links booklink

Contact Us: admin [ a t ] ucptt.com