简体   繁体   中英

Check if there is navigation, and present and push from a SwiftUI view

I am experimenting with SwiftUI. I already have a UIKit based app working, and I want to integrate a SwiftUI view. I achieve to show this SwiftUI view by using UIHostingController.

In this SwiftUI, I intercept a button action. In this action, I want to:

  1. Check if there is a navigation controller (what it used to be self.navigationController on UIKit)
  2. Be able to present or push (through self.navigationController) a new UIKit view controller from this SwiftUI view.

I cannot find any way to achieve any of these 3 things on SwiftUI

You could do this multiple ways: delegates, closures or heck even with combine publishers. The easiest way to get started is with an action closure I think. It could look something like this:

struct SwiftUIView: View {
    let action: () -> Void
    
    var body: some View {
        Button("press me", action: action)
    }
}

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let swiftUIView = SwiftUIView(action: handleButtonPress)
        
        let hostingController = UIHostingController(rootView: swiftUIView)
        addChild(hostingController)
        
        hostingController.view.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(hostingController.view)
 
        NSLayoutConstraint.activate([
            hostingController.view.topAnchor.constraint(equalTo: view.topAnchor),
            hostingController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            hostingController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            hostingController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor)
        ])
    }
    
    func handleButtonPress() {
        print("TODO: Insert navigation controller logic here")
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM