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

标签 ios swift viewcontroller

我正在尝试添加床笠 (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/

相关文章:

ios - 启用地址栏的 Safari ViewController

ios - 如何在 Flutter 应用程序上使用 iOS 显示本地通知?

android - Flutter:错误:找不到 Getter: 'suspending' 。案例 AppLifecycleState.suspending

ios - Swift 4 数据到 UIImage 作为 PNG

html - Paypal Checkout 窗口在 WKWebView(iOS) 中立即关闭

arrays - UITableView SearchBar过滤查询

swift - 有没有办法从 Swift 3 中的异步闭包中抛出错误?

cocoa - 在 Swift 中创建 CTTypesetter

ios - 在 View Controller 之间传递数据

ios - TableView 没有出现在模拟器中?