如何将文件名传递给另一个ViewController?

问题描述 投票:0回答:1

我想从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个回答
0
投票
String(contentsOfFile:)

不返回文件名,但

let fileName = (audiopath as NSString).lastPathComponent

确实

© www.soinside.com 2019 - 2024. All rights reserved.