I am having difficulties raising an event from a UICollectionViewCell
to a handler which exists within a parent UIViewController
. I am trying to detect a LongPress upon a UICollectionViewCell
and fire a method in the UIViewController
- placing the view into an "Edit Mode" show/hiding UI elements and reloading etc. To do so my UIViewController
method must call a data access layer and thus it makes sense the method is placed at this level in the view hierarchy.
I have followed the official documentation and techniques from Xamarin and the great advice from Adam Kemp here.
There are so many conflicting methodologies for achieving this in Xamarin.IOS. Alas, at present, none seem to work in my case. I must be doing something fundamentally wrong!
I have a custom class which inherits from UICollectionViewCell
which contains a private UILongPressGestureRecognizer _recognizer;
and method named UpdateCell
as follows:
public virtual void UpdateCell(bool CanEdit)
{
// Other properties and logic omitted for brevity (no relevance to question)
this._recognizer = new UILongPressGestureRecognizer();
this._recognizer.MinimumPressDuration = 0.5;
this._recognizer.AddTarget(this, new ObjCRuntime.Selector("OnEditModeActivated"));
this.AddGestureRecognizer(_recognizer);
}
I want the Long press to fire the following method within my UIViewController:
[Export("OnEditModeActivated")]
public async void OnEditModeActivated()
{
if (recognizer.State == UIGestureRecognizerState.Ended)
{
this._canEdit = true;
// call Data Access layer using await
_source.Dispose(); // dispose of current UICollectionViewSource object
_source = new UICollectionViewSource(_data, _canEdit);
UICollectionView.Source = _source;
}
}
The line causing issues is
`this._recognizer.AddTarget(this, new ObjCRuntime.Selector("OnEditModeActivated:"));`
The selector should point to the parent UIViewController
which has a public void OnEditModeActivated
decorated with [Export("OnEditModeActivated:")]
.
I can see the compiler running into the "unrecognized selector" exception due to this
(1st arg passed to AddTarget
) obviously being the UICollectionViewCell rather than the UIViewController
and thus it will look to resolve AddTarget
by UICollectionViewCell (instance) -> OnEditActivated (does not exist).
Instead of this
I need to reference the parent UIViewController
but I don't want to pass it by reference to the UpdateCell
method - this would mean passing the reference through many layers thus my code becoming somewhat spaghetti like.
If anyone can explain a better way, by all means feel free. I looked at raising an event within the UICollectionViewCell
instance but to no avail. Moreover I can't seem to get the delegation pattern right in Xamarin!
The problem is the hierarchy I am using being: UIViewController -> UICollectionView -> UICollectionViewSource -> UICollectionViewCell
.
I am sure it is possible to raise an event/call delegate/call method and resolve at the view controller level...
Thanks for taking the time to help on this.
All the best,
John