setValueForKey error

I’m trying to implement an UIAlertController customization to show a switch control for one of the actions as described at https://medium.com/@maximbilan/ios-uialertcontroller-customization-5cfd88140db8#.3xe001k06

My Java class for SwitchAlertActionViewController look like this.
I am not completely sure what annotations (if any) I need to add there and also not sure what commented out swift line below would correspond to in the Java code.

public class SwitchAlertActionViewController extends UIViewController {
  // @IBOutlet weak var valueSwitch: UISwitch!
  UISwitch valueSwitch;
  boolean isSwitchOn = false; 
	
  public static native SwitchAlertActionViewController alloc();

  @Override
  public native SwitchAlertActionViewController init();

  protected SwitchAlertActionViewController(Pointer peer) {
    super(peer);
  }

  public void viewDidLoad() {
    super.viewDidLoad();
    valueSwitch.setOn(isSwitchOn);
 }
}

I also copied corresponding xib file from https://github.com/maximbilan/UIAlertController-Customization/tree/master/AlertControllerCustomization/Sources

Now, when I call UIAlertAction.setValueForKey:

UIViewController switchController = SwitchAlertActionViewController.alloc() //
  .initWithNibNameBundle("SwitchAlertActionViewController", NSBundle.mainBundle());
	
UIAlertAction combinedAction = UIAlertAction.actionWithTitleStyleHandler("Combined", UIAlertActionStyle.Default, null);
combinedAction.setValueForKey(switchController, "contentViewController");

I get the following error.
Could you please advise what am I doing wrong?

2016-12-27 16:06:59.417 MyApp[74409:6637335] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<SwitchAlertActionViewController 0x7bcf2350> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key valueSwitch.'
*** First throw call stack:
(
	0   CoreFoundation                      0x0794dbf2 __exceptionPreprocess + 194
	1   libobjc.A.dylib                     0x06e0ce66 objc_exception_throw + 52
	2   CoreFoundation                      0x0794db21 -[NSException raise] + 17
	3   Foundation                          0x0449f90d -[NSObject(NSKeyValueCoding) setValue:forUndefinedKey:] + 238
	4   Foundation                          0x043cd476 _NSSetUsingKeyValueSetter + 162
	5   Foundation                          0x043ccfe8 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285
	6   UIKit                               0x04a43a9e -[UIViewController setValue:forKey:] + 80
	7   Foundation                          0x043eeb5f -[NSObject(NSKeyValueC
oding) setValue:forKeyPath:] + 309
	8   UIKit                               0x04cf13ad -[UIRuntimeOutletConnection connect] + 138
	9   libobjc.A.dylib                     0x06e22177 -[NSObject performSelector:] + 51
	10  CoreFoundation                      0x078ed180 -[NSArray makeObjectsPerformSelector:] + 272
	11  UIKit                               0x04cefa6c -[UINib instantiateWithOwner:options:] + 2102
	12  UIKit                               0x04a4ad23 -[UIViewController _loadViewFromNibNamed:bundle:] + 437
	13  UIKit                               0x04a4b770 -[UIViewController loadView] + 197
	14  UIKit                               0x04a4bae9 -[UIViewController loadViewIfRequired] + 188
	15  UIKit                               0x04a4c50d -[UIViewController view] + 29
	16  UIKit                               0x04ec66ca -[_UIAlertControllerActionView _loadContentViewControllerView] + 118
	17  UIKit                               0x04ec7932 -[_UIAlertControllerActionView setAction:] + 847
	18  UIKit       
                        0x05479ab3 -[_UIAlertControllerView _actionsChanged] + 668
	19  UIKit                               0x04c404c8 -[UIAlertController loadView] + 356
	20  UIKit                               0x04a4bae9 -[UIViewController loadViewIfRequired] + 188
	21  UIKit                               0x04a4c50d -[UIViewController view] + 29
	22  UIKit                               0x04a68d3e -[UIViewController _setPresentationController:] + 117
	23  UIKit                               0x04a5f314 -[UIViewController _presentViewController:modalSourceViewController:presentationController:animationController:interactionController:completion:] + 1528
	24  UIKit                               0x04a6133c -[UIViewController _presentViewController:withAnimationController:completion:] + 5467
	25  UIKit                               0x04a64632 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 363
	26  UIKit                               0x04a6499b -[UIViewController _performCoordina
tedPresentOrDismiss:animated:] + 535
	27  UIKit                               0x04a64480 -[UIViewController presentViewController:animated:completion:] + 182
	28  MOE                                 0x066471d8 ffi_call_i386 + 24
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Dear Eugene,

xibs and storyboards access properties through KVC. For MOE to add KVC support for a property, you need to bind it as:

@Selector("valueSwitch")
@Property
@IBOutlet
public native UISwitch valueSwitch();

Note the @Property annotation. This makes sure proper backing for the object is allocated in Objective-C, thus the method is native.

In case you also need the setter method, use this:

@Selector("setValueSwitch:")
public native void setValueSwitch(UISwitch value);

Best Regards,
Kristóf

Thank you Kristóf,

I changed switch action view controller as you advised. Also my code that calls it look like this:

UIViewController switchController = SwitchAlertActionViewController.alloc() //
  .initWithNibNameBundle("SwitchAlertActionViewController", NSBundle.mainBundle());
	
UIAlertAction combinedAction = UIAlertAction.actionWithTitleStyleHandler("Combined", UIAlertActionStyle.Default, null);
combinedAction.setValueForKey(switchController, "contentViewController");
	
UIAlertController alert = UIAlertController.alertControllerWithTitleMessagePreferredStyle(null, null, UIAlertControllerStyle.ActionSheet);
alert.addAction(combinedAction);
presentViewControllerAnimatedCompletion(alert, true, null);

But now I’m getting a different exception. This time it didn’t get to UIAlertAction.setValueForKey() call and failed on the UIViewController init. Any idea what am I doing wrong? I checked and my .xib does have valueSwitch connection, but I am total newbie in ios development.
You could use github repo from my first message to take a pick at the original ios code.

2017-01-02 11:34:04.149 XXX[20909:10464435] -[SwitchAlertActionViewController _setViewDelegate:]: unrecognized selector sent to instance 0x7d499450

2017-01-02 11:34:04.185 XXX[20909:10464435] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SwitchAlertActionViewController _setViewDelegate:]: unrecognized selector sent to instance 0x7d499450'
*** First throw call stack:
(
	0   CoreFoundation                      0x07935bf2 __exceptionPreprocess + 194


	1   libobjc.A.dylib                     0x06df4e66 objc_exception_throw + 52
	2   CoreFoundation                      0x079af3bc -[NSObject(NSObject) doesNotRecognizeSelector:] + 172
	3   CoreFoundation                      0x078b44fc ___forwarding___ + 1052
	4   CoreFoundation                      0x078b40be _CF_forwarding_prep_0 + 14
	5   UIKit                               0x04a3da4f +[UIViewController setViewController:forView:] + 53
	6   UIKit                               0x04a34981 -[UIViewController setView:] + 564
	7   Foundation                          0x043b5476 _NSSetUsingKeyValueSetter + 162
	8   Foundation                          0x043b4fe8 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285
	9   UIKit                               0x04a2ba9e -[UIViewController setValue:forKey:] + 80
	10  Foundation                          0x043d6b5f -[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 309
	11  UIKit                               0x04cd93ad -[UIRuntimeOutletConnection connect] + 138
	12  li
bobjc.A.dylib                     0x06e0a177 -[NSObject performSelector:] + 51
	13  CoreFoundation                      0x078d5180 -[NSArray makeObjectsPerformSelector:] + 272
	14  UIKit                               0x04cd7a6c -[UINib instantiateWithOwner:options:] + 2102
	15  UIKit                               0x04a32d23 -[UIViewController _loadViewFromNibNamed:bundle:] + 437
	16  UIKit                               0x04a33770 -[UIViewController loadView] + 197
	17  UIKit                               0x04a33ae9 -[UIViewController loadViewIfRequired] + 188
	18  UIKit                               0x04a3450d -[UIViewController view] + 29
	19  UIKit                               0x04eae6ca -[_UIAlertControllerActionView _loadContentViewControllerView] + 118
	20  UIKit                               0x04eaf932 -[_UIAlertControllerActionView setAction:] + 847
	21  UIKit                               0x05461ab3 -[_UIAlertControllerView _actionsChanged] + 668
	22  UIKit                               0x04c284c8 -[
UIAlertController loadView] + 356
	23  UIKit                               0x04a33ae9 -[UIViewController loadViewIfRequired] + 188
	24  UIKit                               0x04a3450d -[UIViewController view] + 29
	25  UIKit                               0x04a50d3e -[UIViewController _setPresentationController:] + 117
	26  UIKit                               0x04a47314 -[UIViewController _presentViewController:modalSourceViewController:presentationController:animationController:interactionController:completion:] + 1528
	27  UIKit                               0x04a4933c -[UIViewController _presentViewController:withAnimationController:completion:] + 5467
	28  UIKit                               0x04a4c632 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 363
	29  UIKit                               0x04a4c99b -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 535
	30  UIKit                               0x04a4c480 -[UIViewController presentViewController:animate
d:completion:] + 182
	31  MOE                                 0x0662f1d8 ffi_call_i386 + 24
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Dear Eugene,

Could you please share your project with us (or any project where we can reproduce this issue)?

Best Regards,
Kristóf

Thank you for looking into this. Here is a small test project. The error happen when you tap on “…” button.

UIAlertController.zip (122.2 KB)

I’m trying to make this UI:

Dear Eugene,

you set the custom class of the view in the xib file to SwitchAlertActionViewController, which was incorrect. Please remove the custom class.

Best Regards,
Kristóf

Wou! I can’t believe it was that simple. Thank you a lot for your help and for a great product!