gpt4 book ai didi

iOS 如何从另一个 View Controller Swift 调用 func

转载 作者:搜寻专家 更新时间:2023-11-01 07:02:17 26 4
gpt4 key购买 nike

我有一个问题,我想从 firstVC 调用一个方法到 seccondVC 我尝试使用这样的代码:

let storyboard: UIStoryboard = UIStoryboard.init(name: "MainScreenVC", bundle: nil)
let firstViewController: MainScreenVC = storyboard.instantiateViewController(withIdentifier: "MainScreenVC") as! MainScreenVC
firstViewController.automatingFullFill()

但我立即收到错误消息,这是怎么回事?

我的错误:

'Could not find a storyboard named 'MainScreenVC' in bundle NSBundle

最佳答案

如果您为每个 View Controller 使用单独的 Storyboard,您必须提供 Storyboard名称和 View Controller 名称。

let storyboard: UIStoryboard = UIStoryboard.init(name: "give storyboard name here ", bundle: nil)
let firstViewController: MainScreenVC = storyboard.instantiateViewController(withIdentifier: "viewcontrollername here") as! MainScreenVC
firstViewController.automatingFullFill()

(或)你也可以这样调用它

 let mainScreenVC = UIStoryboard.init(name: "give Storyboard name here", bundle: nil).instantiateViewController(withIdentifier: "viewcontroller") as! MainScreenVC

关于iOS 如何从另一个 View Controller Swift 调用 func,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50442935/

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