Swift:从 NSItemProvider 内部更改 UITextField 值
Swift: Change UITextField Value from inside NSItemProvider
我一整天都在和这个问题作斗争,你可以看到我是 Swift 的新手。
我正在尝试更新 iOS 操作扩展中的 UITextField。正确的值记录在下面,并且在 loadItem() 之外可以设置正确的值。
出风口变弱?某种封闭的东西?函数异步触发,我不允许在完成后以这种方式更新 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
/*===================================*/
}
}
}
}
}
尝试像这样更改主队列中的文本字段文本:
DispatchQueue.main.async {
// work that impacts the user interface
self.bookmarkTitle.text = self.pageTitle
}
我一整天都在和这个问题作斗争,你可以看到我是 Swift 的新手。
我正在尝试更新 iOS 操作扩展中的 UITextField。正确的值记录在下面,并且在 loadItem() 之外可以设置正确的值。
出风口变弱?某种封闭的东西?函数异步触发,我不允许在完成后以这种方式更新 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
/*===================================*/
}
}
}
}
}
尝试像这样更改主队列中的文本字段文本:
DispatchQueue.main.async {
// work that impacts the user interface
self.bookmarkTitle.text = self.pageTitle
}