UIActionSheet in NativeScript 1.7

UIActionSheet in NativeScript 1.7

本文关键字:NativeScript in UIActionSheet      更新时间:2023-09-26

我使用ns1.7,我试图调用UIActionSheet。nsapi已经有了dialog .action(),它类似于UIActionSheet,除了它没有破坏性按钮。所以我想尝试使用本地ActionSheet,我已经成功了:

export function editTap(args: GestureEventData) {
   var obj = <any>args.object;
   var actionSheet = new UIActionSheet();
   actionSheet.addButtonWithTitle("Edit");
   actionSheet.addButtonWithTitle("Delete");
   actionSheet.addButtonWithTitle("Cancel");
   actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1;
   actionSheet.destructiveButtonIndex = actionSheet.numberOfButtons - 2;
   actionSheet.showFromRectInViewAnimated(obj.page._nativeView.frame, obj.ios, true);
}

然而,我不知道如何实现回调(这是UIActionSheetDelegate我猜)响应什么用户点击。有一个功能在iOS API称为actionSheet:clickedButtonAtIndex,但在我不知道如何通过javascript(或typescript)调用它。任何形式的帮助都是感激的。

非常感谢。

从Objective-C到JavaScript的NativeScript封送(数据转换)示例代码可以在此链接

找到基本上,为了实现你的委托,你需要这样做:
var UIActionSheetDelegate = UIActionSheet.extend({
    clickedButtonAtIndex(buttonAtIndex) {
        // clickedButtonAtIndex code implementation here
    }
}, {
    protocols: [UIActionSheetDelegate]
});

我找到了另一种方法来实现这一点,并通过使用UIAlertController作为UIActionSheet自iOS 8.3以来已被弃用而成功。这是我所做的:

    var alertController = UIAlertController.alertControllerWithTitleMessagePreferredStyle("title", "message", UIAlertControllerStyle.UIAlertControllerStyleActionSheet);
    var editAction = UIAlertAction.actionWithTitleStyleHandler("Edit", UIAlertActionStyle.UIAlertActionStyleDefault, (arg: UIAlertAction) => {
        //code implementation here
    });
    var deleteAction = UIAlertAction.actionWithTitleStyleHandler("Delete", UIAlertActionStyle.UIAlertActionStyleDestructive, (arg: UIAlertAction) => {
        //code implementation here
    });
    var cancelAction = UIAlertAction.actionWithTitleStyleHandler("Cancel", UIAlertActionStyle.UIAlertActionStyleCancel, (arg: UIAlertAction) => {
        //code implementation here
    });
    alertController.addAction(editAction);
    alertController.addAction(deleteAction);
    alertController.addAction(cancelAction);
    var currentPage = topmost().currentPage;
    var viewController: UIViewController = currentPage.ios;
    viewController.presentModalViewControllerAnimated(alertController, true);