Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 保存文件后如何重新加载或更新viewController的视图_Ios_Objective C_Nsxmlparser_Nsxmlparserdelegate - Fatal编程技术网

Ios 保存文件后如何重新加载或更新viewController的视图

Ios 保存文件后如何重新加载或更新viewController的视图,ios,objective-c,nsxmlparser,nsxmlparserdelegate,Ios,Objective C,Nsxmlparser,Nsxmlparserdelegate,在文档文件夹中保存文件后,是否可以重新加载/更新viewController的视图? 如果是,请建议如何做到这一点 我试图在AppDelegate中保存一个文件,但由于它涉及XML解析,因此只有在加载视图时才会保存该文件。这是应该显示文件内容的视图。但是,由于在保存文件之前加载了该视图,因此该视图不会显示任何内容 这个问题与我现在的问题有关 我可以在这里再次分享代码吗?我可以指导您完成整个过程。您需要实施的是: 当文件写入存储器时,您可以通过NotificationCenter发布通知。此外,您

在文档文件夹中保存文件后,是否可以重新加载/更新viewController的视图? 如果是,请建议如何做到这一点

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

这个问题与我现在的问题有关


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

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

当文件写入存储器时,您可以通过NotificationCenter发布通知。此外,您可以在发布通知之前立即检查文件是否存在。 添加一个观察者,以便通过ViewController中的NotificationCenter(可能在viewDidLoad中)收听该通知。 添加观察者时,需要指定一个selectormethod,该方法将在通知发生时调用。因此,添加一个方法并将其作为addObserver的选择器提供。 在该方法中执行所需的操作,并在该方法的最后一步将视图标记为脏。通常通过self.view.setNeedsDisplay完成。这将完成重新加载/更新部分。 编辑:添加了一些代码示例

我在这里发布相关代码。关注代码内部的注释

AppDelegate.swift

ViewController.swift

CustomXMLParser.swift


到底什么是必须重载的?我在AppDelegate中保存一个文件,然后加载一个viewController,我检查文件是否存在,如果是,将文件的内容加载到视图中。否则将显示一条消息,说明没有可显示的内容。但当我调试应用程序时,我发现我在AppDelegate中的方法是在加载和显示视图后创建和保存文件,并且视图显示一条nothing to show消息。希望我能解释我的问题。我没有重载任何内容。请将文件内容加载到视图中。是。我正在尝试这样做,但我只加载特定的内容。由于文件已经过XML解析,它包含NSDictionary对象,我只加载某些键的值。谢谢@nayem,我已经尝试过了。没用。谢谢你的帮助。给我一些时间,我正在尝试实现一个演示。当然。我会等的。@Prashant,嘿,我已经做了演示,但用的是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)
        }
    }
}
@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()
}
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)")
        }

    }
}