也可以用於Thread中程式, 通知Main Thread 更新畫面的動作
解決方案來源:
Swift - 透過 NotificationCenter 監聽特定的事件同時傳值
override func viewDidLoad()
{
MyFunNtfInit()
}
func MyFunNtfInit()
{
NotificationCenter.default.addObserver(forName: Notification.Name(rawValue: "MYFUNNTF"),
object: nil, queue: nil, using: MyFunNtf)
}
func MyFunNtfPost()
{
NotificationCenter.default.post(name: Notification.Name(rawValue: "MYFUNNTF"),
object: nil, userInfo: ["MSG1":"F","MSG1":"MyMsg2"])
}
@onjc fileprivate func MyFuncThread
{
while( 1)
{
if( bDoNtfPost == true)
{
MyFunNtfPost()
}
}
}
func MyFunNtf(noti:Notification) -> Void
{
guard let userInfo = noti.userInfo,
let sMsg1 = userInfo["MSG1"] as? String,
let sMsg2 = userInfo["MSG2"] as? String else {
return
}
if( sMsg1 == "MyMsg1" )
{
...
}
}
沒有留言:
張貼留言