条形按钮项目未在SWIFT中显示

时间:2023-01-16 12:20:03

I created a simple Bar button item in my project but it is not showing when I run it. This is how I declare it

我在项目中创建了一个简单的Bar按钮项,但是当我运行它时它没有显示。这是我如何宣布它

@IBOutlet weak var songSelectionBar: UIBarButtonItem!

@IBOutlet弱var songSelectionBar:UIBarButtonItem!

Here is a screen shot of my project. I am fairly new to swift so I know I am doing something that should be simple (or maybe not).

这是我的项目的屏幕截图。我对swift很新,所以我知道我正在做一些应该简单(或者可能不是)的事情。

条形按钮项目未在SWIFT中显示

Here is some more code from the project. This function returns the contents of the directory. My understanding is that I should use a tableview to display the output of this function. Can anyone point me to an example or show me how to do this? Thanks

这是项目中的一些代码。此函数返回目录的内容。我的理解是我应该使用tableview来显示这个函数的输出。任何人都可以给我一个例子或告诉我如何做到这一点?谢谢

func getMusicFilesInDirectory() -> [String] {        
    //var wavFiles:[String]
    // We need just to get the documents folder url
    let documentsUrl =  NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!

    // now lets get the directory contents (including folders)
    do {
        let directoryContents = try NSFileManager.defaultManager().contentsOfDirectoryAtURL(documentsUrl, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions())
        print(directoryContents)

    } catch let error as NSError {
        print(error.localizedDescription)
    }
    // now filter the directory to extract only Wav Files

    do {
        let directoryUrls = try  NSFileManager.defaultManager().contentsOfDirectoryAtURL(documentsUrl, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions())
        print(directoryUrls)
        let wavFilesDir = directoryUrls.filter(){ $0.pathExtension! == "wav" }.map{ $0.lastPathComponent! }
        wavFiles = ["Wav Music Files:\n" + wavFilesDir.description]
       } catch let error as NSError {
        print(error.localizedDescription)
    }
     return wavFiles
                                       }

1 个解决方案

#1


1  

If you just declare the IBOutlet,not drag it out from IB,you should get rid of the "weak".
The IB has a strong reference to it's IBOutlet,so you must use it's IBOutlets with weak.
By contrast,if you manually declare an IBOutlet,you must keep a strong reference to it.

如果你只是声明IBOutlet,而不是从IB中拖出它,你应该摆脱“弱”。 IB强烈引用它的IBOutlet,所以你必须使用它的弱IBOutlets。相反,如果手动声明IBOutlet,则必须对其进行强引用。

#1


1  

If you just declare the IBOutlet,not drag it out from IB,you should get rid of the "weak".
The IB has a strong reference to it's IBOutlet,so you must use it's IBOutlets with weak.
By contrast,if you manually declare an IBOutlet,you must keep a strong reference to it.

如果你只是声明IBOutlet,而不是从IB中拖出它,你应该摆脱“弱”。 IB强烈引用它的IBOutlet,所以你必须使用它的弱IBOutlets。相反,如果手动声明IBOutlet,则必须对其进行强引用。