ios - 如何在框架中获取Viewcontroller引用

标签 ios swift frameworks

我有一个应用程序,其应用程序结构如下。

  • 申请
    • FirstViewController
    • 第二个 View Controller
  • ABC框架
    • ABCViewController

在我的ABCFramework中,我需要 FirstViewController 对象,但我无法在框架内导入或找到 FirstViewController。

最佳答案

该框架应该独立工作,而不应绑定(bind)到您当前的应用程序。

实现此目的的最佳方法是使用应用程序的委托(delegate)模式来响应框架触发的事件。下面是用户登录应用程序的示例

所以你可以这样做:

protocol ABCFrameworkProfileDelegate: AnyObject {
    func userDidLogin()
    func authenticationFailed(withError: Error)
}

然后在您的应用程序中:

import ABCFramework

// maybe on button tap?
let authService = ABCAuthService()
authService.login(username: "user", password: "pwd")

// once the login request completes either this or the failed method should be called.
func userDidLogin() {
    let vc = FirstViewController() 
    push(vc, animated: true)
}

这样您的应用程序就可以根据发生的事件来管理要显示的 View 。

关于ios - 如何在框架中获取Viewcontroller引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58375946/

相关文章:

ios - 我什么时候可以使用带有通用链接的 SFSafariViewController、WKWebView 或 UIWebView?

ruby-on-rails - 使用 Phonegap 作为 Rails 3 应用程序的本地容器

ios - 更改 StatusBar 背景颜色和 preferredStatusBarStyle 的最佳方法应该是什么?

ios - 上传 Build On Fabric(Crashlytics) 以进行测试分发

swift - 如何为现有项目创建 App Clip iOS 14

ios - 如何从自定义单元格类更改数据源

swift - UITableView 变小

PHP框架和安全

node.js - Remix.run 不使用 node.js 作为后端吗?

frameworks - 推荐在线免费增值服务基础设施的开箱即用解决方案?