首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >德尔福+ iOS:"UIContextualAction“接口从ObjectiveC到Delphi的”翻译“错误

德尔福+ iOS:"UIContextualAction“接口从ObjectiveC到Delphi的”翻译“错误
EN

Stack Overflow用户
提问于 2017-12-26 22:49:34
回答 1查看 149关注 0票数 2

我试图在Delphi中为UITableView添加交换操作,方法是启用iOS 11中提供的两个UITableView委托方法:

ObjectiveC:

代码语言:javascript
复制
- (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类的定义:

代码语言:javascript
复制
@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导入如下所示:

代码语言:javascript
复制
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的实现,而是以UIContextualActionHandler的形式使用nil创建我的操作。如果我想创建一个“空”操作,该应用程序就会崩溃:

代码语言:javascript
复制
var
  btnDelete: UIContextualAction;
begin
  //Crash by this code line:
  btnDelete := TUIContextualAction.Wrap(TUIContextualAction.OCClass.contextualActionWithStyle(UIContextualActionStyleNormal, StrToNSStr('Delete'), nil));

我做错了什么?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-27 11:20:39

解决方案:

代码语言:javascript
复制
@property (nonatomic, readonly) UIContextualActionStyle style;
@property (nonatomic, copy, readonly) UIContextualActionHandler handler;

是ReadOnly,不需要声明。正确定义:

代码语言:javascript
复制
  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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47984525

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档