如何从SwiftUI按钮调用UIKit/UIController/Storyboard中编写的函数



我是一个SwiftUI新手,正在努力将SwiftUI功能添加到我现有的UIKit/Storyboard代码中。我想从SwiftUI按钮调用UIKit函数。非常感谢您的帮助。以下是为本次讨论而简化的相关代码。从下面的代码中,我想从SwiftUI中的If语句中调用函数startAction((和stopAction((。。。

if (startStop_flag) {
//******** call StartAction()
} else {
//******* call StopAction()
}

下面的整个代码。一些上下文:当应用程序运行时,屏幕的下半部分将显示"UIkit情节提要视图";并显示按钮";打开Swift Container View";。当用户单击此按钮时,将打开SwiftUI容器视图。该视图将显示";这是一个swiftUI视图";并显示一个按钮";启动/停止";。当用户单击此按钮时,需要调用StartAction((或StopAction((。这两个函数位于UIViewController中。我希望我清楚这个问题和要求。

ViewController.swift 
class ViewController: UIViewController {
@IBOutlet weak var nativeView: UIView!
@IBOutlet weak var nativeView_Label: UILabel!
@IBOutlet weak var nativeView_openSwiftViewBtn: UIButton!
@IBOutlet weak var containerView_forSwiftUI: UIView!


@IBSegueAction func embedSwiftUIView(_ coder: NSCoder) -> UIViewController? {
return UIHostingController(coder: coder, rootView: SwiftUIView2(text: "Container View"))
}

var toggleOpenCloseContainerView : Bool = false

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.


containerView_forSwiftUI.isHidden = true
}
@IBAction func openContainerView_touchInside(_ sender: Any) {

if (toggleOpenCloseContainerView) {
containerView_forSwiftUI.isHidden = false
toggleOpenCloseContainerView = false
nativeView_openSwiftViewBtn.setTitle("Close Swift Container View", for: .normal)
} else {
containerView_forSwiftUI.isHidden = true
toggleOpenCloseContainerView = true
nativeView_openSwiftViewBtn.setTitle("Open Swift Container View", for: .normal)
}

}


// These two functions need to be called from the SwiftUI's button.

func startAction() {

print("Start Action called from SwiftUI's button")
}

func stopAction() {

print("Stop Action called from SwiftUI's button")
}

}

swiftUI功能在此文件中


struct SwiftUIView2: View {

var text: String
@State var startStop_flag: Bool = true

var body: some View {
VStack {
Text(text)
HStack {
Image(systemName: "smiley")
Text("This is a SwiftUI View")
Spacer()

Button("(startStop_flag ? "Start": "Stop")") {
startStop_flag = !startStop_flag

if (startStop_flag) {

//******** call StartAction()

} else {

//******* call StopAction()

}

}   .padding()
.background(Color.red)
.cornerRadius(40)
.foregroundColor(.white)
.padding(5)
.overlay(
RoundedRectangle(cornerRadius: 40)
.stroke(Color.red, lineWidth: 1)
)
}
}
.font(.largeTitle)
.background(Color.blue)

}

}
struct SwiftUIView_Previews: PreviewProvider {
static var previews: some View {
SwiftUIView2(text: "Sample Text")
}
}

您可以为此使用闭包。首先,在SwiftUI视图中定义并调用它们。

struct SwiftUIView2: View {

var text: String
var startAction: (() -> Void) /// define closure 1
var stopAction: (() -> Void) /// define closure 2

...
...

Button("(startStop_flag ? "Start": "Stop")") {
startStop_flag = !startStop_flag

if (startStop_flag) {
//******** call StartAction()
startAction()
} else {
//******* call StopAction()
stopAction()
}
}
}

然后,只需在ViewController.swift中分配闭包的内容。

@IBSegueAction func embedSwiftUIView(_ coder: NSCoder) -> UIViewController? {
return UIHostingController(
coder: coder,
rootView: SwiftUIView2(
text: "Container View",
startAction: { [weak self] in
self?.startAction()
},
stopAction: { [weak self] in
self?.stopAction()
}
)
)
}

这里是从SwiftUI按钮按下打开UIkit ViewController的最简单方法。

Button("Next Page") {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootViewController = storyboard.instantiateViewController(withIdentifier: "ProfileVC")
if let window = UIApplication.shared.windows.first {
window.rootViewController!.present(rootViewController, animated: true)
}
}
}

最新更新