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

看板MacDev作者 (一個人踢球的小正)時間6年前 (2018/01/08 20:38), 編輯推噓5(5012)
留言17則, 6人參與, 6年前最新討論串1/1
我在一個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://www.ptt.cc/bbs/MacDev/M.1515415128.A.C07.html

01/08 20:45, 6年前 , 1F
DispatchQueue.main.async{ table.text = value }試試
01/08 20:45, 1F

01/08 20:45, 6年前 , 2F
01/08 20:45, 2F

01/08 22:41, 6年前 , 3F
UI 更新應該是要放在main thread 沒錯
01/08 22:41, 3F

01/09 09:40, 6年前 , 4F
你指定delegate時,是指給另一個你初始化出來的 viewC
01/09 09:40, 4F

01/09 09:40, 6年前 , 5F
ontrollerB
01/09 09:40, 5F

01/09 11:38, 6年前 , 6F
一樓大大~照做了可是沒用耶Orz
01/09 11:38, 6F

01/09 11:38, 6年前 , 7F
cryinglove大~那我應該如何做才對呢?
01/09 11:38, 7F

01/09 12:51, 6年前 , 8F
這行確定有進去嗎? itemPriceLabel!.text = price 設
01/09 12:51, 8F

01/09 12:51, 6年前 , 9F
中斷看一下
01/09 12:51, 9F

01/09 13:13, 6年前 , 10F
有進去但是label的值都沒有改變
01/09 13:13, 10F

01/09 13:31, 6年前 , 11F
看起來你 ViewControllerB() 這裏弄出來另一個 viewcontr
01/09 13:31, 11F

01/09 13:31, 6年前 , 12F
ollerb 物件而不是顯示在螢幕上那個要不要考慮改用 notif
01/09 13:31, 12F

01/09 13:31, 6年前 , 13F
ication 實作?
01/09 13:31, 13F

01/09 14:31, 6年前 , 14F
剛剛改用了notifacation了~可是這中間會頓一下
01/09 14:31, 14F

01/09 14:33, 6年前 , 15F
點擊按鈕傳送通知到改變Label大約要4秒
01/09 14:33, 15F

01/09 14:45, 6年前 , 16F
甩笨了...自己多調用了別的func,使用notifacation
01/09 14:45, 16F

01/09 14:46, 6年前 , 17F
後就成功實現了~感謝樓上諸位大大的幫忙^^
01/09 14:46, 17F
文章代碼(AID): #1QKsPOm7 (MacDev)