情节提要 (<UIStoryboard: 0x600002c51ec0>) 不包含标识符为“AlphabetController”的视图控制器

Storyboard (<UIStoryboard: 0x600002c51ec0>) doesn't contain a view controller with identifier 'AlphabetController''

提问人:Johnny Nguyên 提问时间:8/6/2023 最后编辑:Johnny Nguyên 更新时间:8/11/2023 访问量:44

问:

 @objc func abcPressed() {
        DispatchQueue.main.async { [weak self] in
            guard let self = self else { return }
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let controller = storyboard.instantiateViewController(withIdentifier: AlphabetController.storyboardIdentifier) as! AlphabetController
            
            controller.modalPresentationStyle = .fullScreen
            self.present(controller, animated: true, completion: nil)
        }
    }

当我按下 abcPressed 时,出现错误“Storyboard (<UIStoryboard: 0x600002c51ec0>) doesn't contain a view controller with identifier 'AlphabetController”

如何通过代码识别AlphabetController?

请帮帮我!谢谢

class AlphabetController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    var collectionView: UICollectionView!
    let alphabet = ["A", "Ă", "Â", "B", "C", "D", "Đ", "E", "Ê", "G", "H", "I", "K", "M", "N", "O", "Ô", "Ơ", "P", "Q", "R", "S", "T", "U", "Ư", "V", "X", "Y"]
    var audioPlayer: AVAudioPlayer?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        }

我试过了

extension AlphabetController {
    static var storyboardIdentifier: String {
        return String(describing: self)
    }
Swift Storyboard 标识符

评论

2赞 Sweeper 8/6/2023
你有没有添加到你的故事板中?AlphabetController
0赞 Johnny Nguyên 8/6/2023
你如何添加它?
0赞 HangarRash 8/6/2023
在情节提要中设置标识符。
0赞 Johnny Nguyên 8/6/2023
你能给出更详细的说明吗?@HangarRash
2赞 lorem ipsum 8/6/2023
UIViewController 不会自动成为情节提要,您必须将其分配给情节提要。这通常是在任何教程的前几个步骤之一中完成的,例如 Apple 的“iOS 开发教程”,我建议查看它们,这将使学习过程比猜测方式简单得多。

答:

0赞 DonMag 8/11/2023 #1

在情节提要中设置标识符:

enter image description here

请注意,它可以是任何字符串 - 例如,可以使用:“alphaVC” - 它不需要与 View Controller 类的名称匹配。