Swift Error quot;UIButton.currentTitle must be used from main thread onlyquot;(Swift 错误“UIButton.currentTitle 必须仅在主线程中使用)
问题描述
我刚刚从以下链接关注了这个你管教程:
网址:
导入 UIKit导入媒体播放器类 GenreButtonScreen: UIViewController {var musicPlayer = MPMusicPlayerController.applicationMusicPlayer@IBAction funcgenreButtonTapped(_ sender: UIButton) {MPMediaLibrary.requestAuthorization {(状态)在如果状态 == .authorized {self.playGenre(流派:sender.currentTitle!)}}}@IBAction func stopButtonTapped(_ sender: UIButton) {音乐播放器.stop()}@IBAction func nextButtonTapped(_ sender: UIButton) {音乐播放器.skipToNextItem()}功能播放流派(流派:字符串){音乐播放器.stop()让查询 = MPMediaQuery()让谓词 = MPMediaPropertyPredicate(值:流派,forProperty:MPMediaItemPropertyGenre)查询 .addFilterPredicate(谓词)musicPlayer.setQueue(with: query)musicPlayer.shuffleMode = .songs音乐播放器.play()}}MPMediaLibrary.requestAuthorization 是异步调用,布局组件只能在主线程中修改.你应该这样使用:
MPMediaLibrary.requestAuthorization { (status) in如果状态 == .authorized {DispatchQueue.main.async {self.playGenre(流派: sender.currentTitle ?? String())}}}I just followed this you tube tutorial from the following link:
URL: https://www.youtube.com/watch?v=aiXvvL1wNUc
I cam across an error saying "UIButton.currentTitle must be used from main thread only"
Not really sure how to fix this as I am new to making apps, can anyone help me?
Greatly appreciated and thanks in advance!
import UIKit
import MediaPlayer
class GenreButtonScreen: UIViewController {
var musicPlayer = MPMusicPlayerController.applicationMusicPlayer
@IBAction func genreButtonTapped(_ sender: UIButton) {
MPMediaLibrary.requestAuthorization { (status) in
if status == .authorized {
self.playGenre(genre: sender.currentTitle!)
}
}
}
@IBAction func stopButtonTapped(_ sender: UIButton) {
musicPlayer.stop()
}
@IBAction func nextButtonTapped(_ sender: UIButton) {
musicPlayer.skipToNextItem()
}
func playGenre(genre:String) {
musicPlayer.stop()
let query = MPMediaQuery()
let predicate = MPMediaPropertyPredicate(value: genre, forProperty: MPMediaItemPropertyGenre)
query .addFilterPredicate(predicate)
musicPlayer.setQueue(with: query)
musicPlayer.shuffleMode = .songs
musicPlayer.play()
}
}
The MPMediaLibrary.requestAuthorization is an async call, and layout components only can be modified in the Main Thread. You should use this way:
MPMediaLibrary.requestAuthorization { (status) in
if status == .authorized {
DispatchQueue.main.async {
self.playGenre(genre: sender.currentTitle ?? String())
}
}
}
这篇关于Swift 错误“UIButton.currentTitle 必须仅在主线程中使用"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Swift 错误“UIButton.currentTitle 必须仅在主线程中使用"
基础教程推荐
- 在 appComponent dagger 2 中动态添加测试模块? 2022-01-01
- - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作 2022-01-01
- Android:STATE_SELECTED不工作 2022-01-01
- 在 iOS8 中无法获得正确的键盘高度值 2022-01-01
- 如何将多个组件添加到 PickerView? 2022-01-01
- 我的 UIImageView 的任务 2022-01-01
- 新的@SystemApi 注解是什么意思,和@hide 有什么区别 2022-01-01
- 在 Android 模拟器中激活网络位置提供程序? 2022-01-01
- 可可/目标C(OSX不是iOS)从子对象访问父对象 2022-01-01
- 突出显示朗读文本(在 iPhone 的故事书类型应用程序中) 2022-01-01
