swift 中的框架代表
Framework Delegates in swift
您好,我创建了一个简单的框架,其中包含一个委托 return 返回一个值给我的主应用程序。
我执行的步骤:
- 在 Xcode
中创建了一个框架
- 创建协议 public
- 在主应用程序项目中导入我的框架并成功集成我的协议
- 但无法从框架中获取值
在框架内
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
}
}
在您的 框架中 修改 registraionView
方法以接受类型 MyDataSendingDelegateProtocol
的 delegate
参数,然后将其设置为 homeVC's
delegate
,即
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
.
您好,我创建了一个简单的框架,其中包含一个委托 return 返回一个值给我的主应用程序。
我执行的步骤:
- 在 Xcode 中创建了一个框架
- 创建协议 public
- 在主应用程序项目中导入我的框架并成功集成我的协议
- 但无法从框架中获取值
在框架内
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
}
}
在您的 框架中 修改 registraionView
方法以接受类型 MyDataSendingDelegateProtocol
的 delegate
参数,然后将其设置为 homeVC's
delegate
,即
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
.