我想在SwiftUI视图中使用带有Timer的视图控制器,但是会崩溃。
class TimeViewController : UIViewController {
var time = 0
var timer : Timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(startTimer), userInfo: nil, repeats: true)
override func viewDidLoad() {
super.viewDidLoad()
startTimer()
}
@objc func startTimer() {
time += 1
print(time)
}
}
struct TimeVC : UIViewControllerRepresentable {
typealias UIViewControllerType = TimeViewController
func makeUIViewController(context: UIViewControllerRepresentableContext<TimeVC>) -> TimeVC.UIViewControllerType {
return TimeViewController()
}
func updateUIViewController(_ uiViewController: TimeVC.UIViewControllerType, context: UIViewControllerRepresentableContext<TimeVC>) {
} }
struct ContentView : View {
var body: some View {
TimeVC()
} }