将以下本地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

最佳答案

由于NativeScript将TypeScript列为一流成员,因此使用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占位符与createView一起使用

至于语法,它看起来应该与此相似,但是仍然必须创建自己的ViewController,并且正如作者所说-遵守AGEmojiKeyboardViewDataSource和AGEmojiKeyboardViewDelegate协议。

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>

关于ios - native 编码错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37664043/

10-17 01:38