gpt4 book ai didi

ios - 保存文件后如何重新加载或更新 View Controller 的 View

转载 作者:行者123 更新时间:2023-11-29 00:18:49 27 4
gpt4 key购买 nike

一旦文件保存在文档文件夹中,是否可以重新加载/更新 viewController 的 View ?如果是,请建议如何完成此任务。

我正在尝试在 AppDelegate 中保存一个文件,但由于它涉及 XML 解析,因此只有在加载 View 时才会保存该文件。这是应该显示文件内容的 View 。但由于它在文件保存之前加载,因此 View 不显示任何内容。

此查询与我在此 link 的问题相关

我要在这里再次分享代码吗?

最佳答案

我可以指导您完成整个过程。您需要实现的是:

  1. 当您的文件写入存储时,您可以通过NotificationCenter发布通知。 (此外,您可以在发布通知之前立即检查该文件是否存在)。
  2. 添加一个观察者,用于通过 ViewController 中的 NotificationCenter 监听该Notification(可能在 viewDidLoad())。
  3. 当您添加观察者时,您需要指定一个选择器(方法),通知发生时将调用该选择器。因此,添加一个方法并将其提供为 addObserver 的选择器
  4. 在该方法中执行所需的操作,并在该方法的最后将您的 View 标记为脏。通常使用 self.view.setNeedsDisplay() 来完成。这将完成重新加载/更新部分。

EDIT: added some code example

这里我发布相关代码。重点关注代码内的注释。

AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

postNotificationAfterFileSave()
return true
}

func postNotificationAfterFileSave() {
// do your job with your custom class's method for saving
// you should get a filepath back
CustomXMLParser().parseAndSave { (filePath) in
if FileManager.default.fileExists(atPath: filePath) {
// if the file exists, post Notification
NotificationCenter.default.post(name: Notification.Name("FileWrittenCompletion"), object: nil)
}
}
}

ViewController.swift

@IBOutlet weak var myLabel: UILabel!

override func viewDidLoad() {
super.viewDidLoad()
// add an observer for your Notification and specify a selector that will be called when Notification is got
NotificationCenter.default.addObserver(self, selector: #selector(updateUI), name: Notification.Name("FileWrittenCompletion"), object: nil)
myLabel.text = "File is being saved"
}

@objc func updateUI() {
// this method will be called when Notification happens
// change your UI objects according to your needs
myLabel.textColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
myLabel.text = "File is saved successfully"
// Finally mark your view `dirty` that will do the reloading task of the UI
self.view.setNeedsDisplay()
}

CustomXMLParser.swift

func parseAndSave(completion: @escaping (String)-> Void ) {
let when = DispatchTime.now() + 10
DispatchQueue.global().asyncAfter(deadline: when) {
let string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
// get the document directory
let fileURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("myFile").appendingPathExtension("txt")

do {
// write to filepath
try string.write(to: fileURL!, atomically: true, encoding: .utf8)

DispatchQueue.main.async {
// return the filePath through completion block
completion((fileURL?.path)!)
}

} catch {
print("\(error.localizedDescription)")
}

}
}

关于ios - 保存文件后如何重新加载或更新 View Controller 的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44561256/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com