nativescript 编组错误

nativescript marshalling error

在将以下本机 iOS 代码编组为 nativescript 时,我似乎遇到了一个奇怪的问题:

CGRect keyboardRect = CGRectMake(0, 0, self.view.frame.size.width, 216);
AGEmojiKeyboardView *emojiKeyboardView = [[AGEmojiKeyboardView alloc] initWithFrame:keyboardRect
                                                                           dataSource:self];
emojiKeyboardView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
emojiKeyboardView.delegate = self;
self.textView.inputView = emojiKeyboardView;

我想出的等价物如下:

var keyboardRect = CGRectMake(0, 0, platform.screen.mainScreen.widthPixels, 216);
var emojiKeyboardView = new AGEmojiKeyboardView();
emojiKeyboardView.frame = keyboardRect;
emojiKeyboardView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
emojiKeyboardView.delegate = this;
views.textInput.ios.inputView = emojiKeyboardView;

平台只是 require("platform");views.textInput 是我需要将 inputView 设置为 AGEmojiKeyboardView 的视图。

我真的不明白我哪里错了。出现的只是一个没有内容的灰色键盘。

编辑:

我把js代码改成如下:

var keyboardRect = CGRectMake(0, 0, uiView.view.frame.size.width, 216);
var emojiKeyboardView = new AGEmojiKeyboardView(keyboardRect, uiView);
emojiKeyboardView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
emojiKeyboardView.delegate = uiView;
views.textInput.ios.inputView = emojiKeyboardView; 

其中 uiView 只是 page.ios,现在似乎发生了一些事情,但仍然不是预期的结果。抛出异常,即:-[UIViewControllerImpl emojiKeyboardView:imageForSelectedCategory:]: unrecognized selector sent to instance 0x7e6bff60

我可能弄错了,但我认为您没有正确使用编组。

文档指出:

AGEmojiKeyboardView *emojiKeyboardView = [[AGEmojiKeyboardView alloc] initWithFrame:keyboardRect dataSource:self];

所以你的 {N} 版本应该是这样的:

var emojiKeyboardView = AGEmojiKeyboardView.alloc().initWithFrameDataSource(keyboardRect, uiView);

这是一篇很棒的博客 - post 关于如何基于 3rd 方库为 nativescript 创建插件。它还逐步解释了这种类型的编组是如何完成的。 http://fluentreports.com/blog/?p=167

由于 NativeScript 将 TypeScript 作为第一个 class 成员,因此使用 TS 确实更容易做一些事情。例如,您还可以使用 META-Generator 只需 运行 这两行即可为您的 CocoaPod(以及所有其他 Objective-C 文件)生成元数据和类型,您不必担心语法是否正确。(您仍然需要了解基本的规则)

TNS_DEBUG_METADATA_PATH="$(pwd)/metadata" tns build ios [--for-device] [--release]

TNS_TYPESCRIPT_DECLARATIONS_PATH="$(pwd)/typings" tns build ios [--for-device] [--release]

此外,如果您想创建自定义视图,最好的变体是使用 NativeScript placeholder创建视图

至于语法,它看起来应该与此类似,但您仍然必须创建自己的 ViewController 并且正如作者所说 - 符合 AGEmojiKeyboardViewDataSourceAGEmojiKeyboardViewDelegate 协议。

var frame = require('ui/frame');
var page;

function onLoaded(args) {
     page = args.object;

}
exports.onLoaded = onLoaded;

function onCreatingView(args) {
    setTimeout(function() {
        var uiView = page.ios.view; // replace with own UIView and conform to AGEmojiKeyboardViewDataSource and AGEmojiKeyboardViewDelegate protocol.

        var frame = {origin: {x:0, y:0}, size: {width: uiView.frame.size.width, height:600}};
        var emojiView = AGEmojiKeyboardView.alloc().initWithFrameDataSource(frame, uiView);
        emojiView.autoresizingMask = UIView.UIViewAutoresizing.UIViewAutoresizingFlexibleHeight;
        emojiView.delegate = uiView;

        var textContainer = NSTextContainer.alloc().initWithSize({width: 80, height: 180});

        var frame = {origin: {x:0, y:0}, size: {width: 100, height:220}};
        var textView = UITextView.alloc().initWithFrameTextContainer(frame, textContainer);
        textView.inputView = emojiView;

        args.view = textView;
    }, 500);
}
exports.onCreatingView = onCreatingView;

page.xml

<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="onLoaded">
  <StackLayout>
        <Placeholder creatingView="onCreatingView" id="placeholder-view"/>
  </StackLayout>
</Page>