Search code examples
iosswiftxcodeviewcontroller

open text files in same ViewController


I am making app for iPhone in Xcode using Swift i have added multiple buttons on Main.Storyboard and there is one txt file for each Button, Like

 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!
        }
    }

This Is My TextViewVC and i have create a new ViewController in storyboard with TextView and linked it with TextViewVC file

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.
    }

Error

enter image description here

but i don't know how to open file when user click on button, it should be something like when user click on button1 the txt1 file should open in ViewController2 and when user click on button2 the txt2 file should open in Same when user click on button1 the txt1 file should open in ViewController as Button1 and the same for rest Buttons..


Solution

  • create a common code for pushing the new viewController.

    have created a opneTextFile method in which you just pass a fileName.

    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!
        }
    }
    

    // action methods

    @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
        }
    
    }
    

    NOTE : you can open different files in a same VC.