【问题标题】:Delphi + iOS: Error by "translating" of "UIContextualAction" interface from ObjectiveC to DelphiDelphi + iOS:将“UIContextualAction”接口从ObjectiveC“翻译”到Delphi时出错
【发布时间】:2017-12-26 22:49:34
【问题描述】:

我正在尝试通过实现 iOS 11 中可用的两个 UITableView 委托方法来为 Delphi 中的 UITableView 添加交换操作:

leadingSwipeActionsConfigurationForRowAtIndexPath trailingSwipeActionsConfigurationForRowAtIndexPath

目标C:

- (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);

动作派生自新类 UIContextualAction。我的问题正是在这门课的翻译上。这是 ObjectiveC 类的定义:

@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

因此,我的 Delphi 导入如下所示:

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>);

目前我什至没有来实现 UIContextualActionHandler 并使用 nil 作为 UIContextualActionHandler 创建我的操作。如果我想创建“空”操作,应用程序会崩溃:

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

我做错了什么? 谢谢!

【问题讨论】:

  • 文档没有说你可以通过 nil (除非它可能允许你无论如何)。您是否尝试过提供处理程序?
  • @DaveNottage 否,但 Xcode 测试应用程序使用 nil 作为处理程序没有问题。我现在正在尝试创建处理程序。处理程序看起来像一个匿名方法,它仍然有另一个匿名方法作为参数。你知道我如何在 Delphi 中得到它吗?

标签: ios delphi interface firemonkey uicontextualaction


【解决方案1】:

解决方案:

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

是只读的,不必声明。正确定义:

  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;

【讨论】:

    猜你喜欢
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    相关资源
    最近更新 更多