Search code examples
iosswiftwatchkitapple-watch

How to get input text from WKInterfaceTextField in watchOS?


I have one textfield like:

@IBOutlet weak var tfAmount: WKInterfaceTextField!

and I want the input text from tfAmount on button Action. So, How can I get?


Solution

  • To receive the text entered by the user, connect the text field in the storyboard to an action method defined in your interface controller.

    Obj-C

    - (IBAction)textFieldAction:(NSString*) value
    

    Swift

    @IBAction func textFieldAction(_ value: NSString?)
    

    WatchKit calls the action method after the user dismisses the text input controller. The value parameter contains the string entered by the user. If the user cancels the text input controller, the value is nil.

    https://developer.apple.com/documentation/watchkit/wkinterfacetextfield?language=swift