gpt4 book ai didi

ios - 我应该在 ViewController 中的什么位置放置初始化代码?

转载 作者:行者123 更新时间:2023-11-29 05:40:51 25 4
gpt4 key购买 nike

我正在尝试添加床笠 (Repo)对于我的应用程序,在他们的使用说明中,他们只有这段代码,没有进一步说明将其放在哪里。

let controller = MyViewController()

let sheetController = SheetViewController(controller: controller)

// It is important to set animated to false or it behaves weird currently
self.present(sheetController, animated: false, completion: nil)

我想知道该代码应该放在哪里,以便我可以在我的应用程序中包含 FittedSheet 的基本版本。任何输入将不胜感激,我是 swift 的新手,我的应用程序中的所有后端数据都可以正常工作,但很难显示它。

最佳答案

MyViewController() 是一个 Controller 名称,其数据需要使用 FittedSheet 库来呈现。让我举个例子。

在下面的示例中,我在项目中创建了 customSheetController。我在 Storyboard中设计 UI 并为其创建一个 swift 类。

customSheetController storyboard UI

下面是customSheetController swift类语法

class customSheetController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}
}

在另一个viewcontroller类中,在 Collection View 项单击时打开该工作表。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let sheetView = storyboard.instantiateViewController(withIdentifier: "customSheetController") as? customSheetController {
let sheetController = SheetViewController(controller: sheetView)

// It is important to set animated to false or it behaves weird currently
self.present(sheetController, animated: false, completion: nil)
}
}

在上面的示例中,我在单击 Collection View 项时打开了一个工作表,您可以在按钮事件或任何要打开该工作表的事件中添加上述代码。

另一个例子

让我再举一个例子,假设你的项目中有两个 viewcontroller 类,比如 A 和 B。你想在 B Controller 上呈现 A Controller 。然后需要修改如下代码。

在B Controller 类中,假设您想在按钮单击时呈现A Controller ,那么您可以在按钮单击事件上编写以下代码

let controller = A()

let sheetController = SheetViewController(controller: controller)

self.present(sheetController, animated: false, completion: nil)

关于ios - 我应该在 ViewController 中的什么位置放置初始化代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56572399/

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