gpt4 book ai didi

ios - 通过在 Collection View 单元格内按下按钮来实例化 Storyboard

转载 作者:行者123 更新时间:2023-11-30 10:57:35 25 4
gpt4 key购买 nike

我试图通过在 Collection View 单元格内按下按钮来实例化 Storyboard,但遇到错误:

class sampleCell: UICollectionViewCell {

@IBAction func infoButtonPressed(_ sender: Any) {

let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "InputViewController") as! InputViewController//error: Use of undeclared type "InputViewController"

present(vc, animated: false)//error:Use of unresolved identifier 'present'
}
}

知道如何做到这一点吗?

最佳答案

1-您没有名为InputViewController的类

let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "InputViewController") as! InputViewController 

2-您不能在单元格内呈现 vc,您需要一个委托(delegate)

present(vc, animated: false) 
<小时/>

仅用于主 Storyboard的注释,您可以替换

UIStoryboard(name: "Main", bundle: nil)

with(但也在 vc 内部)

self.storyboard

编辑: -------------------------------------- -------------------

细胞内部

class sampleCell: UICollectionViewCell {
weak var delegate:CurrentController?
}

然后在cellForItemAt

let cell = /////
cell.delegate = self

添加到vc

func goToInput() {

let vc = self.storyboard!.instantiateViewController(withIdentifier: "InputViewController") as! InputViewController//error: Use of undeclared type "InputViewController"
present(vc, animated: false)

}

在内部单元格操作之后

@IBAction func infoButtonPressed(_ sender: Any) {
delegate?.goToInput()
}

关于ios - 通过在 Collection View 单元格内按下按钮来实例化 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53770475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com