【问题标题】:How to pass file name to another ViewController?如何将文件名传递给另一个 ViewController?
【发布时间】:2020-04-25 08:30:44
【问题描述】:

我想从 ViewController 获取 mp3 文件名并将其传递给另一个名为 TextViewVc 的 VC,目前我正在使用此方法但我无法使用此代码获取文件名

  @IBAction func Button1(_ sender: Any) {

       opneAudioFile(path: "Chalisa")


   }
   @IBAction func Button2(_ sender: Any) {


    opneAudioFile(path: "well")}

这是我获取文件名的代码

func opneAudioFile(path : String) {

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

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


   }

我在哪里做错了?或者还有其他方法可以获取 mp3 的文件名吗?

【问题讨论】:

  • 哪个代码属于哪个控制器?不清楚..请详细说明您的问题

标签: ios swift xcode file mp3


【解决方案1】:
String(contentsOfFile:)

不返回文件名,但是

let fileName = (audiopath as NSString).lastPathComponent

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多