I'm trying to bind a custom command to UIAlertController (ios 8). I have a button in the nav bar and an action sheet attached to it. When user clicks the nav bar button, the action sheet appears. When user clicks the action sheet button, he must be redirected to another view / view model.
My view model code:
public ICommand AddPhonecallCommand
{
get
{
return new MvxCommand(() => ShowViewModel<AddPhonecallViewModel>();
}
}
public ICommand AddMeetingCommand
{
get
{
return new MvxCommand(() => ShowViewModel<AddMeetingViewModel>();
}
}
My view code:
var actionSheet = UIAlertController.Create("Add a new...", null, UIAlertControllerStyle.ActionSheet);
actionSheet.AddAction(UIAlertAction.Create("Phone call", UIAlertActionStyle.Default, null));
actionSheet.AddAction(UIAlertAction.Create("Meeting", UIAlertActionStyle.Default, null));
var rightNavButton = new UIBarButtonItem("Add", UIBarButtonItemStyle.Plain, (s, e) =>
{
this.PresentViewController(actionSheet, true, null);
});
So, every button in the action sheet should redirect to a specific view model. But it seems that buttons in the action sheet are not UIButtons. So I need somehow bind UIAlertAction to ICommand.
set.Bind(...).For(...).To(vm => vm.AddPhonecallCommand);
What should I put instead of the dots?
I don’t have an instance of the view model in the view. I’m using this syntax:
var set = this.CreateBindingSet<MainView, MainViewModel>();
set.Bind(source).To(vm => vm.Events); // binding IEnumerable
set.Apply();
There is no direct instantiation of the viewmodel. The framework does all dirty work for me. So, if I'm writing
set.Bind(rightNavButton).To(vm => vm.AddPhonecallCommand); // binding the Clicked event
everything works just fine. But if I'm tryng to write something like this
var actionSheetButton = UIAlertAction.Create("Phone call", UIAlertActionStyle.Default, null));
...
set.Bind(actionSheetButton).To(vm => vm.AddPhonecallCommand); // attempt to bind
nothing happens. Probably because we don’t have suitable events in UIAlertAction at all.