Get selected index from WKPickerInterface

I can call WKInterfacePicker to call pickerDidSettle , but how can I get the index of the selected item?

 @IBAction override func pickerDidSettle(picker: WKInterfacePicker) { print("Picker settled to index: \(picker)") } 
+7
ios watch-os-2 apple-watch
source share
2 answers

Each time the selection value changes, the WKInterfacePicker object reports a change in the action method associated with it. The format of this action method is as follows:

OBJECTIVE-C

- (IBAction) pickerAction: (NSInteger) Index

SWIFT @IBAction func pickerAction (index: Int)

You can use the index value of the action methods to get the selected item from the array of elements that you used to configure the collector. the collector reports every change in the method of action, no matter how quickly the user rotates the digital crown. If your application should only respond to an element selected by the user, use the pickerDidSettle: WKInterfaceController method to get the selected element.

Source Link

UPDATED SOURCE OF SOURCES

+5
source share

What have I done to solve this problem?

Create in .h file

 NSInteger PickerIndex; 

fast

  var PickerIndex: Int 

Then in my .m

  - (IBAction)pickerAction:(NSInteger)index { PickerIndex = index; } 

fast

 @IBAction func pickerAction(index: Int) { PickerIndex = index } 

* Remember to link IBAction in Interface Builder

And then use it in pickerDidSettle method

 -(void)pickerDidSettle:(WKInterfacePicker *)picker{ NSLog(@"Selected Index: %ld",(long)PickerIndex); } 

fast

 func pickerDidSettle(_ picker: WKInterfacePicker) { print(PickerIndex) } 
0
source share

All Articles