Swift 错误“UIButton.currentTitle 必须仅在主线程中使用"

Swift Error quot;UIButton.currentTitle must be used from main thread onlyquot;(Swift 错误“UIButton.currentTitle 必须仅在主线程中使用)
本文介绍了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 必须仅在主线程中使用"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)