在相同 ViewController 中打开文本文件

open text files in same ViewController

我正在使用 Swift 在 Xcode 中为 iPhone 制作应用程序 我在 Main.Storyboard 上添加了多个按钮,每个按钮都有一个 txt 文件,喜欢

 class ViewController: UIViewController {

    @IBAction func Button1(_ sender: Any) {

           opneTextFile(fileName: "example")
       }
       @IBAction func Button2(_ sender: Any) {

           opneTextFile(fileName: "example1")

       }
override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    func opneTextFile(fileName : String) {

        if let filepath = Bundle.main.path(forResource: fileName, ofType: "txt") {
            do {
                let contents = try String(contentsOfFile: filepath)
                print(contents)
                //  Now push second ViewController form here with contents.
                   if let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "TextViewVC") as? TextViewVC {
                    secondVC.content = contents
                    self.navigationController?.pushViewController(secondVC, animated: true)
                }
            } catch {

                // contents could not be loaded
            }
        } else {
            // example.txt not found!
        }
    }

这是我的 TextViewVC,我在故事板中使用 TextView 创建了一个新的 ViewController 并将其与 TextViewVC 文件链接

class TextViewVC: UIViewController {

     @IBOutlet var textView: UITextView!
     var content : String?

    override func viewDidLoad() {
        super.viewDidLoad()

        textView.text = content

        // Do any additional setup after loading the view.
    }

错误

但我不知道当用户点击按钮时如何打开文件,它应该类似于当用户点击按钮 1 时 txt1 文件应该在 ViewController2 中打开,当用户点击按钮 2 时当用户单击按钮 1 时,txt2 文件应以相同的方式打开 txt1 文件应以 ViewController 与 Button1 相同的方式打开,其余按钮也应以相同的方式打开..

您应该提取文本数据并像显示任何其他字符串一样显示它。

        let path = Bundle.main.path(forResource: "terms", ofType: "txt")!
        let content = try! String(contentsOfFile: path, encoding: .utf8)

请注意,这是一个快速有效的解决方案,您应该使用 try catch 块,否则您的应用程序会在尝试时崩溃!失败。

创建用于推送新 viewController 的通用代码。

创建了一个 opneTextFile 方法,您只需在其中传递一个文件名。

func opneTextFile(fileName : String) {

    if let filepath = Bundle.main.path(forResource: fileName, ofType: "txt") {
        do {
            let contents = try String(contentsOfFile: filepath)
            print(contents)
            //  Now push second ViewController form here with contents. 
               if let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "ViewController2") as? ViewController2 {
                secondVC.content = contents
                self.navigationController?.pushViewController(secondVC, animated: true)
            }
        } catch {

            // contents could not be loaded
        }
    } else {
        // example.txt not found!
    }
}

// 动作方法

@IBAction func Button1(_ sender: Any) {

    opneTextFile(fileName: "example")
}
@IBAction func Button2(_ sender: Any) {

    opneTextFile(fileName: "example1")

}

// ViewController2

class ViewController2: UIViewController {

    @IBOutlet weak var txt_view: UITextView!
    var content : String?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        txt_view.text = content
    }

}

注意:您可以在同一个 VC 中打开不同的文件。