【问题标题】:Framework Delegates in swiftswift中的框架代表
【发布时间】:2020-07-16 09:45:43
【问题描述】:

您好,我创建了一个带有委托的简单框架,用于将值返回给我的主应用程序。

我做的步骤:

  1. 在 Xcode 中创建了一个框架
  2. 创建了一个公共协议
  3. 在主应用项目中导入我的框架并成功集成我的协议
  4. 但无法从框架中获取价值

在框架中

public protocol MyDataSendingDelegateProtocol: NSObject {
func sendDataToFirstViewController(myData: String)
}
public var delegate: MyDataSendingDelegateProtocol? = nil

发送值表单框架为

delegate?.sendDataToFirstViewController(myData: "hello world")

在主应用中

class ViewController: UIViewController, MyDataSendingDelegateProtocol{

func sendDataToFirstViewController(myData: String) {
    print("from frame work \(myData)")
   }
}

访问我的框架 VC

public class OOB_View : UIViewController {

public func registraionView() -> UIViewController {
    let storyboard = UIStoryboard.init(name: "main", bundle: Bundle(for: oobRegVc.self))
    let homeVC = storyboard.instantiateViewController(withIdentifier: "view") as! oobRegVc
    return homeVC
   }
 }

【问题讨论】:

  • 添加相关代码。
  • @PGDev 现在检查我的问题
  • 你在哪里将代理设置为 ViewController?
  • 我不知道在哪里设置..帮助我
  • 你如何访问框架的方法?必须有委托存在的类吗?

标签: ios swift xcode frameworks


【解决方案1】:

在您的框架中修改registraionView方法以接受MyDataSendingDelegateProtocol类型的delegate参数,然后将其设置为homeVC'sdelegate,即

public class OOB_View : UIViewController {
    public func registraionView(delegate: MyDataSendingDelegateProtocol) -> UIViewController {
        let storyboard = UIStoryboard.init(name: "main", bundle: Bundle(for: oobRegVc.self))
        let homeVC = storyboard.instantiateViewController(withIdentifier: "view") as! oobRegVc
        homeVC.delegate = delegate
        return homeVC
    }
}

现在,在您的主应用中,像这样调用方法registraionView(delegate:),

let oobClass = OOB_View()
let x = oobClass.registraionView(delegate: self)
x.modalPresentationStyle = .fullScreen
self.present(x, animated: true, completion: nil)

上面的代码必须在ViewController的某处符合MyDataSendingDelegateProtocol。

【讨论】:

    猜你喜欢
    • 2021-11-03
    • 2018-07-02
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多