LoginSignup
3
3

More than 5 years have passed since last update.

RoboVMでxibを使う方法

Last updated at Posted at 2013-11-17

はじめに

画面レイアウトをコードで記述するのは大変です。そこでInterfaceBuilderで作ったレイアウトをRoboVMから使ってみます。
参考にしたのはこちらです。
http://blog.robovm.org/2013/02/robovm-interface-builder-integration.html

Viewを作成

Xcode5でViewを作成します。
この時、storyboardではなくxib形式で作成します。
メニューのFile-New-File..でファイルを新規作成します。
Choose a template for your new file: というダイアログが出たら、左側で-iOS-UserInterfaceを選択し、右側のViewを選んでNextを押します。
ファイル名を入力して出来上がり。

部品を配置

作ったxibファイルに部品を配置します。

クラスを作成

UIViewControllerを継承したクラスを作成し、配置した部品を関連付けます。

xibファイルをコンパイル

ibtoolコマンドでxibファイルをコンパイルし、nib形式のファイルを作成します。

ibtool --errors --warnings --notices --output-format human-readable-text --compile TestViewController.nib MyFourthApp/TestViewController.xib

mikumikustudio-gdxプロジェクトにコピー

出来たnibファイルをプロジェクトにコピーします。

cp TestViewController.xib mikumikustudio-gdx/ios/src/main/resources

JavaでTestViewControllerクラスを作成する。

@CustomClass("TestViewController")
public class TestViewController extends UIViewController{
    UILabel label;
    public TestViewController() {
        super("TestViewController", null);
    }
    private void sayHello(UIButton button) {
        label.setText("Hello!!");
    }

    @SuppressWarnings("unused")
    @Callback
    @BindSelector("click:")
    private static void sayHello(TestViewController self, Selector sel, UIButton button) {
        self.sayHello(button);
    }
    @SuppressWarnings("unused")
    @Callback
    @BindSelector("setLabel:")
    @TypeEncoding("v@:@")
    private static void setLabel(TestViewController self, Selector sel, UILabel label) {
        self.label = label;
    }
    @SuppressWarnings("unused")
    @Callback
    @BindSelector("label")
    private static UILabel getLabel(TestViewController self, Selector sel) {
        return self.label;
    }
}

この例ではUILabel1個とUIButton1個を配置しています。
UIButtonが押されたらUILabelにHello!!とセットしています。
@CustomClassアノテーションでObjective CのTestViewControllerクラスを自動生成しています。

注意点としては、propertyのメソッドに@TypeEncoding("v@:@")を付ける事です。
関連スレッド: https://groups.google.com/d/msg/robovm/aYVfjS2Y32s/3RS7drHQ0I0J

終わりに

RoboVMからxibファイルを使う方法を解説しました。
プロパティの作成など、ちょっと面倒くさいのが難点です。
ただこれは単純作業ですので将来的にはxibからJavaのクラスを自動生成するようにすれば解決出来るかもしれません。実際、RoboVMに標準で付いているcocoatouchのラッパークラスは雛形を自動生成して作っているようです。
RoboVMでUIを作る方法としては他にJavaFXを使う方法もあります。こちらはiOS以外でも動きますので、もしかするとこちらの方が良いかもしれません。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3