iosdelphiinterfacefiremonkeyuicontextualaction

Delphi + iOS: Error by "translating" of "UIContextualAction" interface from ObjectiveC to Delphi


i'm trying to add swap actions for UITableView in Delphi by imlement two UITableView delegate methods available from iOS 11:

leadingSwipeActionsConfigurationForRowAtIndexPath trailingSwipeActionsConfigurationForRowAtIndexPath

ObjectiveC:

- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);

The actions derives from the new class UIContextualAction. My problem is exactly in the translation of this class. Here is the ObjectiveC class definition:

@class UIContextualAction;

// call the completionHandler to reset the context to its normal state (e.g. when swiping, resets to unswiped state)
// pass YES to the completionHandler if the action was actually performed, to show a visual indication of the successful completion

typedef void (^UIContextualActionHandler)(UIContextualAction *action, __kindof UIView *sourceView, void(^completionHandler)(BOOL actionPerformed));

typedef NS_ENUM(NSInteger, UIContextualActionStyle) {
    UIContextualActionStyleNormal,
    UIContextualActionStyleDestructive
} NS_SWIFT_NAME(UIContextualAction.Style) API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);

UIKIT_EXTERN API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos)
@interface UIContextualAction : NSObject

+ (instancetype)contextualActionWithStyle:(UIContextualActionStyle)style title:(nullable NSString *)title handler:(UIContextualActionHandler)handler;

@property (nonatomic, readonly) UIContextualActionStyle style;
@property (nonatomic, copy, readonly) UIContextualActionHandler handler;

@property (nonatomic, copy, nullable) NSString *title;
@property (nonatomic, copy, nullable) UIColor *backgroundColor; // a default background color is set from the action style
@property (nonatomic, copy, nullable) UIImage *image;

@end

Accordingly, my Delphi import looks like this:

const
  UIContextualActionStyleNormal = 0;
  UIContextualActionStyleDestructive = 1;

type
  UIContextualActionStyle = integer;

  UIContextualActionClass = interface(NSObjectClass)
    ['{F341A178-2950-4D0A-9EF2-DCDEAB76FF81}']
    function contextualActionWithStyle(style: UIContextualActionStyle; title: NSString; handler: Pointer{UIContextualActionHandler}): Pointer; cdecl;
  end;

  UIContextualAction = interface(NSObject)
    ['{C5B4CB53-0655-41FA-B48E-D4FB0E9A54FB}']
    function title: NSString; cdecl;
    function backgroundColor: UIColor; cdecl;
    function image: UIImage; cdecl;
    function style: UIContextualActionStyle;
    function handler: Pointer{UIContextualActionHandler};
    procedure setTitle(title: NSString); cdecl;
    procedure setBackgroundColor(backgroundColor: UIColor); cdecl;
    procedure setImage(image: UIImage); cdecl;
    procedure setStyle(style: UIContextualActionStyle); cdecl;
    procedure setHandler(handler: Pointer{UIContextualActionHandler}); cdecl;
  end;
  TUIContextualAction = class(TOCGenericImport<UIContextualActionClass, UIContextualAction>);

I didn't even come to the implementation of the UIContextualActionHandler and create my action with nil as UIContextualActionHandler for the moment. The app crashes if I want to create an "empty" action:

var
  btnDelete: UIContextualAction;
begin
  //Crash by this code line:
  btnDelete := TUIContextualAction.Wrap(TUIContextualAction.OCClass.contextualActionWithStyle(UIContextualActionStyleNormal, StrToNSStr('Delete'), nil));

What am I doing wrong? Thanks!


Solution

  • Solution:

    @property (nonatomic, readonly) UIContextualActionStyle style;
    @property (nonatomic, copy, readonly) UIContextualActionHandler handler;
    

    are ReadOnly and don't have to be declared. Correct definition:

      UIContextualActionClass = interface(NSObjectClass)
        ['{F341A178-2950-4D0A-9EF2-DCDEAB76FF81}']
        function contextualActionWithStyle(style: UIContextualActionStyle; title: NSString; handler: Pointer{UIContextualActionHandler}): Pointer; cdecl;
      end;
    
      UIContextualAction = interface(NSObject)
        ['{C5B4CB53-0655-41FA-B48E-D4FB0E9A54FB}']
        function title: NSString; cdecl;
        function backgroundColor: UIColor; cdecl;
        function image: UIImage; cdecl;
        procedure setTitle(title: NSString); cdecl;
        procedure setBackgroundColor(backgroundColor: UIColor); cdecl;
        procedure setImage(image: UIImage); cdecl;
      end;