English 中文(简体)
传真: 传真:@un.org
原标题:SwiftUI button not called on UIInputViewController

我正在SiOS习俗主机推广工作。 页: 1 无国界医生组织正在适当展示,但从未被称作!

import SwiftUI

class KeyboardViewController: UIInputViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let vc = UIHostingController(rootView: MyKeyButtons())
        vc.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.addSubview(vc.view)
    }
}


struct MyKeyButtons: View {

    let data: [String] = ["A", "B", "C"]
    var body: some View {
        HStack {
            ForEach(data, id: .self) { aData in

                Button(action: {
                    print("button pressed!") // Not working!
                }) {
                    Text(aData).fontWeight(.bold).font(.title)
                        .foregroundColor(.white).padding()
                        .background(Color.purple)
                }
            }
        }
    }
}

为便于理解,此处全文如下:

问题回答

我拿不出它来印刷,但可以肯定的是工作。 你只是需要把一项职能交给《观点》,并在纽伦行动部分内将其称为:

let vc = UIHostingController(root观点: MyKeyButtons(btnPressed: { 
    self.dismissKeyboard() // dismiss keyboard action to test button press        
}

观点:

struct MyKeyButtons: View {

    let data: [String] = ["A", "B", "C"]
    let btnPressed: () -> Void
    
    var body: some View {
        HStack {
            ForEach(data, id: .self) { aData in

                Button(action: {
                    btnPressed()
                }) {
                    Text(aData).fontWeight(.bold).font(.title)
                        .foregroundColor(.white).padding()
                        .background(Color.purple)
                }
            }
        }
    }
}

You can obviously pass the pressed letter as well:

let vc = UIHostingController(root观点: MyKeyButtons(keyPressed: { letter in
    self.textDocumentProxy.insertText(letter) // append letter to the end of the text input        
}

Change in the view:

let keyPressed: (_ letter: String) -> Void


Button(action: {
          keyPressed(aData)
       }) {
          Text(aData).fontWeight(.bold).font(.title)
          .foregroundColor(.white).padding()
          .background(Color.blue)
       }




相关问题
SwiftUI: How do I position a button x points above a sheet?

I am trying to recreate a Maps-style UI with a persistent bottom sheet and controls fixed above. Apple Maps UI with controls above ☝️ Example here, the "Look Around" binoculars button and ...

BraintreeDropIn deprecated functions error

I m getting this error on the latest release of BraintreeDropIn, I really don t get it since based on the patch notes, this should ve been addressed already, I have already tried cleaning derived data,...

how to limit zoom level in map swift ui?

how to limit zoom level in map swift ui i have map and need to make zoom level limited Map(coordinateRegion: $region,annotationItems: viewModel.nearbyGyms) { item in ...

UIImageView - How to get the file name of the image assigned?

Is it possible to read the name of an UIImageView s UIImage that s presently stored in the UIImageView? I was hoping you could do something kind of like this, but haven t figured it out. NSString *...

热门标签