我一整天都在和这个做斗争,你可以看到我是斯威夫特的新手。
我试图在一个UITextField动作扩展中更新一个iOS。下面是正确的值日志,在loadItem()之外,可以设置正确的值。
出口中的弱变量?某种结束的事情?函数异步触发,并且不允许在UI完成后以这种方式更新UI?
提前谢谢你!
class ActionViewController: UIViewController {
//outlets
@IBOutlet weak var bookmarkTitle: UITextField!
//vars
var pageTitle = ""
override func viewDidLoad() {
super.viewDidLoad()
if let inputItem = extensionContext!.inputItems.first as? NSExtensionItem {
if let itemProvider = inputItem.attachments?.first as? NSItemProvider {
itemProvider.loadItem(forTypeIdentifier: kUTTypePropertyList as String) { [unowned self] (dict, error) in
// do stuff!
let itemDictionary = dict as! NSDictionary
let javaScriptValues = itemDictionary[NSExtensionJavaScriptPreprocessingResultsKey] as! NSDictionary
print(javaScriptValues)
//this works, gets correct data. how to assign to IBOutlet?
self.pageTitle = javaScriptValues["title"] as! String
NSLog("Title From JS Preprocessor: " + self.pageTitle)
/*===================================*/
//Reference to property 'bookmarkTitle' in closure requires explicit 'self.' to make capture semantics explicit
//bookmarkTitle.text = self.pageTitle
//when the line below is used, the action extension fails to even open
self.bookmarkTitle.text = self.pageTitle
/*===================================*/
}
}
}
}
}发布于 2018-02-01 05:50:40
尝试更改主队列中的文本,如下所示:
DispatchQueue.main.async {
// work that impacts the user interface
self.bookmarkTitle.text = self.pageTitle
}https://stackoverflow.com/questions/48555672
复制相似问题