作者whitefox0102 (一個人踢球的小正)
看板MacDev
標題[問題] swift delegate傳值後顯示在label
時間Mon Jan 8 20:38:44 2018
我在一個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顯示出來卻一直顯示不出來
請大大們解惑
感謝~~
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 219.70.218.213
※ 文章網址: https://webptt.com/m.aspx?n=bbs/MacDev/M.1515415128.A.C07.html
1F:推 gundam00: DispatchQueue.main.async{ table.text = value }試試 01/08 20:45
2F:→ gundam00: 看 01/08 20:45
3F:推 Sexyfather: UI 更新應該是要放在main thread 沒錯 01/08 22:41
4F:推 cryinglove: 你指定delegate時,是指給另一個你初始化出來的 viewC 01/09 09:40
5F:→ cryinglove: ontrollerB 01/09 09:40
6F:→ whitefox0102: 一樓大大~照做了可是沒用耶Orz 01/09 11:38
7F:→ whitefox0102: cryinglove大~那我應該如何做才對呢? 01/09 11:38
8F:推 RINPE: 這行確定有進去嗎? itemPriceLabel!.text = price 設 01/09 12:51
9F:→ RINPE: 中斷看一下 01/09 12:51
10F:→ whitefox0102: 有進去但是label的值都沒有改變 01/09 13:13
11F:推 booray: 看起來你 ViewControllerB() 這裏弄出來另一個 viewcontr 01/09 13:31
12F:→ booray: ollerb 物件而不是顯示在螢幕上那個要不要考慮改用 notif 01/09 13:31
13F:→ booray: ication 實作? 01/09 13:31
14F:→ whitefox0102: 剛剛改用了notifacation了~可是這中間會頓一下 01/09 14:31
15F:→ whitefox0102: 點擊按鈕傳送通知到改變Label大約要4秒 01/09 14:33
16F:→ whitefox0102: 甩笨了...自己多調用了別的func,使用notifacation 01/09 14:45
17F:→ whitefox0102: 後就成功實現了~感謝樓上諸位大大的幫忙^^ 01/09 14:46