如何知道 UIKit 视图控制器中的 swiftUI 事件? ||如何提供 swiftUI 和 uikit 之间的通信
How to know swiftUI event in UIKit view controller? || How to provide communication between swiftUI and uikit
我是 swiftUI 的新手。我想在我当前的 UIkit 项目中添加 swiftUI 视图,我为此创建了一个演示,现在我卡在了其中。
这是我的 ViewController 代码:
import UIKit
import SwiftUI
class ViewController: UIViewController {
@IBOutlet weak var stepperContenerView: UIView!
@IBOutlet weak var btn:UIButton!
lazy var stepView = StepperView(intialCount: 1)
lazy var swiftUIHostingView = UIHostingController(rootView: stepView)
override func viewDidLoad() {
super.viewDidLoad()
stepperContenerView.addSubview(swiftUIHostingView.view)
addChild(swiftUIHostingView)
// stepView.delegate = self
swiftUIHostingView.view.frame = stepperContenerView.bounds
stepperContenerView.addConstrained(subview: swiftUIHostingView.view)
swiftUIHostingView.didMove(toParent: self)
}
@IBAction func onClickBtn(_ sender: UIButton) {
let alert = UIAlertController(title: "Stepper count", message: "Current value : \(stepView.getCount())", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "ok", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
//extension ViewController:OnClickStepperButton {
// func updateCount(count: Int) {
// print("Test")
// lbl.text = "Stepper count is - \(count)"
// }
//}
这是我的 swiftUI 步进器视图代码:-
import SwiftUI
protocol OnClickStepperButton {
func updateCount(count:Int)
}
struct StepperView: View {
@State private var count:Int = 1
var delegate:OnClickStepperButton?
init(intialCount:Int){
self.count = intialCount
}
var body: some View {
HStack(alignment: .center, spacing: 10) {
Button("+", action: {
count += 1
print(count)
// delegate?.updateCount(count: count)
})
Text("\(count)")
.frame(minWidth: 40,minHeight: 40)
.background(Color.white)
.foregroundColor(.black)
.scaledToFill()
Button("-", action: {
if count > 1 {
count -= 1
print(count)
}
// delegate?.updateCount(count: count)
})
}
.font(.system(size: 22, weight: .medium, design: .serif))
.frame(minWidth: 120, minHeight: 40, alignment: .center)
.background(Color.blue)
.foregroundColor(Color.white)
.cornerRadius(20)
}
public func getCount() -> Int {
count
}
}
struct Stepper_Previews: PreviewProvider {
static var previews: some View {
StepperView(intialCount: 1)
.previewLayout(.fixed(width: 300, height: 70))
}
}
既没有委托调用,也没有在单击按钮时获取更新值。 (代表们有意发表评论以强调其不起作用,请取消评论以进行检查)。
delegate
变量永远不会按原样设置。
您需要删除 ?从 SwiftUI 视图中的变量,因此您被迫在初始化时提供一个值。
此外,删除自定义初始化程序。
我是 swiftUI 的新手。我想在我当前的 UIkit 项目中添加 swiftUI 视图,我为此创建了一个演示,现在我卡在了其中。
这是我的 ViewController 代码:
import UIKit
import SwiftUI
class ViewController: UIViewController {
@IBOutlet weak var stepperContenerView: UIView!
@IBOutlet weak var btn:UIButton!
lazy var stepView = StepperView(intialCount: 1)
lazy var swiftUIHostingView = UIHostingController(rootView: stepView)
override func viewDidLoad() {
super.viewDidLoad()
stepperContenerView.addSubview(swiftUIHostingView.view)
addChild(swiftUIHostingView)
// stepView.delegate = self
swiftUIHostingView.view.frame = stepperContenerView.bounds
stepperContenerView.addConstrained(subview: swiftUIHostingView.view)
swiftUIHostingView.didMove(toParent: self)
}
@IBAction func onClickBtn(_ sender: UIButton) {
let alert = UIAlertController(title: "Stepper count", message: "Current value : \(stepView.getCount())", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "ok", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
//extension ViewController:OnClickStepperButton {
// func updateCount(count: Int) {
// print("Test")
// lbl.text = "Stepper count is - \(count)"
// }
//}
这是我的 swiftUI 步进器视图代码:-
import SwiftUI
protocol OnClickStepperButton {
func updateCount(count:Int)
}
struct StepperView: View {
@State private var count:Int = 1
var delegate:OnClickStepperButton?
init(intialCount:Int){
self.count = intialCount
}
var body: some View {
HStack(alignment: .center, spacing: 10) {
Button("+", action: {
count += 1
print(count)
// delegate?.updateCount(count: count)
})
Text("\(count)")
.frame(minWidth: 40,minHeight: 40)
.background(Color.white)
.foregroundColor(.black)
.scaledToFill()
Button("-", action: {
if count > 1 {
count -= 1
print(count)
}
// delegate?.updateCount(count: count)
})
}
.font(.system(size: 22, weight: .medium, design: .serif))
.frame(minWidth: 120, minHeight: 40, alignment: .center)
.background(Color.blue)
.foregroundColor(Color.white)
.cornerRadius(20)
}
public func getCount() -> Int {
count
}
}
struct Stepper_Previews: PreviewProvider {
static var previews: some View {
StepperView(intialCount: 1)
.previewLayout(.fixed(width: 300, height: 70))
}
}
既没有委托调用,也没有在单击按钮时获取更新值。 (代表们有意发表评论以强调其不起作用,请取消评论以进行检查)。
delegate
变量永远不会按原样设置。
您需要删除 ?从 SwiftUI 视图中的变量,因此您被迫在初始化时提供一个值。
此外,删除自定义初始化程序。