title: Custom UICollectionViewDelegateFlowLayout - Xamarin IOS
body: <p>I'm trying to create a custom <code>UICollectionViewDelegateFlowLayout</code> in F#, similar to the one below in C#: </p>
  
  <pre><code>public class CityViewDelegate : UICollectionViewDelegateFlowLayout
  {
      #region Application Access
      public static AppDelegate App {
          get { return (AppDelegate)UIApplication.SharedApplication.Delegate; }
      }
      #endregion
  
      #region Constructors
      public CityViewDelegate ()
      {
      }
      #endregion
  
      #region Override Methods
      public override CGSize GetSizeForItem (UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
      {
          return new CGSize (361, 256);
      }
  
      public override bool CanFocusItem (UICollectionView collectionView, NSIndexPath indexPath)
      {
          if (indexPath == null) {
              return false;
          } else {
              var controller = collectionView as CityCollectionView;
              return controller.Source.Cities[indexPath.Row].CanSelect;
          }
      }
  
      public override void ItemSelected (UICollectionView collectionView, NSIndexPath indexPath)
      {
          var controller = collectionView as CityCollectionView;
          App.SelectedCity = controller.Source.Cities [indexPath.Row];
  
          // Close Collection
          controller.ParentController.DismissViewController(true,null);
      }
      #endregion
  }
  }
  </code></pre>
  
  <p>I have done the following: </p>
  
  <pre><code>    [&lt;Register ("CollectionViewFlowDelegate")&gt;]
  type CollectionViewFlowDelegate (handle:IntPtr) = 
      inherit UICollectionViewDelegateFlowLayout (handle)
  
      let mutable parentViewController : UIViewController = null
  
      member this.parent 
          with get() = parentViewController 
          and set(value) = parentViewController &lt;- value
  
      override x.ItemSelected(collectionView : UICollectionView, indexPath : NSIndexPath) = 
          let Storyboard = UIStoryboard.FromName("Main", null)
          let controller = Storyboard.InstantiateViewController("ChatControllerLandlord") :?&gt; ChatControllerLandlord
          controller.GetListingId &lt;- "-L0pJmU9yj4hAocHjnrB"
          controller.GetPartnerId &lt;- "SWs56OIGzMdiCjSXahzDQX8zve92"
          controller.GetLandlordId &lt;- "YMM45tgFFvYB7rx9PhC2TE5eW6D2"
          controller.GetPartnerName &lt;- "Dan"
          parentViewController.PresentViewController(controller,true,null)
  </code></pre>
  
  <p>Next, I changed the delegate for the <code>UICollectionView</code> to the custom one I defined. This crashes with the following exception: </p>
  
  <pre><code>    Objective-C exception thrown.  Name: NSInvalidArgumentException Reason: -[CollectionViewFlowDelegate initWithCoder:]: unrecognized selector sent to instance 0x60400001b840
  Native stack trace:
      0   CoreFoundation                      0x000000010899012b __exceptionPreprocess + 171
      1   libobjc.A.dylib                     0x0000000116260f41 objc_exception_throw + 48
      2   CoreFoundation                      0x0000000108a11024 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
      3   CoreFoundation                      0x0000000108912f78 ___forwarding___ + 1432
      4   CoreFoundation                      0x0000000108912958 _CF_forwarding_prep_0 + 120
      5   UIKit                               0x000000010ce754c8 -[UIClassSwapper initWithCoder:] + 246
      6   UIKit                               0x000000010d0ca109 UINibDecoderDecodeObjectForValue + 704
      7   UIKit                               0x000000010d0c9e3e -[UINibDecoder decodeObjectForKey:] + 89
      8   UIKit                               0x000000010d4acfa1 -[UICollectionView initWithCoder:] + 107
      9   UIKit                               0x000000010d0ca109 UINibDecoderDecodeObjectForValue + 704
      10  UIKit                               0x000000010d0c9e3e -[UINibDecoder decodeObjectForKey:] + 89
      11  UIKit                               0x000000010ce7516a -[UIRuntimeConnection initWithCoder:] + 136
      12  UIKit                               0x000000010d0ca109 UINibDecoderDecodeObjectForValue + 704
      13  UIKit                               0x000000010d0ca2a7 UINibDecoderDecodeObjectForValue + 1118
      14  UIKit                               0x000000010d0c9e3e -[UINibDecoder decodeObjectForKey:] + 89
      15  UIKit                               0x000000010ce74391 -[UINib instantiateWithOwner:options:] + 1262
      16  UIKit                               0x000000010cb8b3ff -[UIViewController _loadViewFromNibNamed:bundle:] + 383
      17  UIKit                               0x000000010cb8bd2b -[UIViewController loadView] + 177
      18  UIKit                               0x000000010d4fb304 -[UICollectionViewController loadView] + 290
      19  UIKit                               0x000000010cb8c05c -[UIViewController loadViewIfRequired] + 195
      20  UIKit                               0x000000010cb8c8b9 -[UIViewController view] + 27
      21  UIKit                               0x000000010d625441 -[_UIFullscreenPresentationController _setPresentedViewController:] + 89
      22  UIKit                               0x000000010cb592a3 -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 133
      23  UIKit                               0x000000010cb9fb67 -[UIViewController _presentViewController:withAnimationController:completion:] + 3808
      24  UIKit                               0x000000010cba29a9 __63-[UIViewController _presentViewController:animated:completion:]_block_invoke + 99
      25  UIKit                               0x000000010d756811 -[_UIViewControllerTransitionCoordinator _applyBlocks:releaseBlocks:] + 294
      26  UIKit                               0x000000010d7523c0 -[_UIViewControllerTransitionContext _runAlongsideCompletions] + 155
      27  UIKit                               0x000000010d752098 -[_UIViewControllerTransitionContext completeTransition:] + 118
      28  UIKit                               0x000000010cb56dba -[UITransitionView notifyDidCompleteTransition:] + 251
      29  UIKit                               0x000000010cb56a31 -[UITransitionView _didCompleteTransition:] + 1397
      30  UIKit                               0x0000000135c4efe5 -[UITransitionViewAccessibility _didCompleteTransition:] + 44
      31  UIKit                               0x000000010cb590c8 -[UITransitionView _transitionDidStop:finished:] + 104
      32  UIKit                               0x000000010ca7a6c4 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 343
      33  UIKit                               0x000000010ca7ad23 -[UIViewAnimationState animationDidStop:finished:] + 293
      34  UIKit                               0x0000000135c8d691 -[UIViewAnimationStateAccessibility animationDidStop:finished:] + 118
      35  UIKit                               0x000000010ca7add7 -[UIViewAnimationState animationDidStop:finished:] + 473
      36  UIKit                               0x0000000135c8d691 -[UIViewAnimationStateAccessibility animationDidStop:finished:] + 118
      37  QuartzCore                          0x00000001198446bd _ZN2CA5Layer23run_animation_callbacksEPv + 323
      38  libdispatch.dylib                   0x0000000116bb51ba _dispatch_client_callout + 8
      39  libdispatch.dylib                   0x0000000116bbf3a4 _dispatch_main_queue_callback_4CF + 1260
      40  CoreFoundation                      0x0000000108952e39 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
      41  CoreFoundation                      0x0000000108917462 __CFRunLoopRun + 2402
      42  CoreFoundation                      0x0000000108916889 CFRunLoopRunSpecific + 409
      43  GraphicsServices                    0x0000000119aaa9c6 GSEventRunModal + 62
      44  UIKit                               0x000000010c9e45d6 UIApplicationMain + 159
      45  ???                                 0x0000000135b45518 0x0 + 5195978008
      46  ???                                 0x0000000135b45153 0x0 + 5195977043
  </code></pre>