我正在我的应用程序中显示一个媒体选择器,并遵循Apples documentation。我想使用UILongPressGestureRecognizer
而不是使用按钮。使用按钮效果很好,而使用手势会导致警告和错误的布局。我是iOS开发的新手,很难理解按钮为何起作用,但手势却不起作用。
我认为解决该问题的方法是在视图层次结构中添加弹出窗口,但是我不确定如何正确执行此操作。我尝试了Stack Overflow的一些其他解决方案,但还没有运气。
2019-04-29 12:40:06.173264-0700 soundswipe[756:83233] Warning: Attempt to present <MPMediaPickerController: 0x10271fb60> on <soundswipe.PlayerViewController: 0x1027130f0> whose view is not in the window hierarchy!
longPress函数出现错误,chooseSongsButtonPressed()
函数运行正常。
@IBAction func longPress(_ sender: UILongPressGestureRecognizer) {
let myMediaPickerVC = MPMediaPickerController(mediaTypes: MPMediaType.music)
myMediaPickerVC.allowsPickingMultipleItems = true
myMediaPickerVC.popoverPresentationController?.sourceView = self.view
myMediaPickerVC.delegate = self
self.present(myMediaPickerVC, animated: true, completion: nil)
}
@IBAction func chooseSongsButtonPressed(_ sender: UIButton) {
let myMediaPickerVC = MPMediaPickerController(mediaTypes: MPMediaType.music)
myMediaPickerVC.allowsPickingMultipleItems = true
myMediaPickerVC.popoverPresentationController?.sourceView = self.view
myMediaPickerVC.delegate = self
self.present(myMediaPickerVC, animated: true, completion: nil)
}