Question

We are porting a libgdx game to ios.
I create a UITextField and set delegate, here is the code:

this.textField.setDelegate(new Delegate(textField));

public static class Delegate extends UITextFieldDelegate.Adapter
{
    private UITextField mTextField;

    public Delegate(UITextField textField)
    {
        this.mTextField = textField;
    }

    @Override
    public void didBeginEditing(UITextField textField) {
        // TODO Auto-generated method stub
        Gdx.app.debug(TAG, "didBeginEditing");
    }

    @Override
    public void didEndEditing(UITextField textField) {
        // TODO Auto-generated method stub
        Gdx.app.debug(TAG, "didEndEditing");
    }

    @Override
    public boolean shouldBeginEditing(UITextField textField) {
        // TODO Auto-generated method stub
        Gdx.app.debug(TAG, "shouldBeginEditing");
        return true;
    }

    @Override
    public boolean shouldChangeCharacters(UITextField textField,
            NSRange range, String string) {
        // TODO Auto-generated method stub
        Gdx.app.debug(TAG, "shouldChangeCharacters");
        return true;
    }

    @Override
    public boolean shouldClear(UITextField textField) {
        // TODO Auto-generated method stub
        Gdx.app.debug(TAG, "shouldClear");
        return true;
    }

    @Override
    public boolean shouldEndEditing(UITextField textField) {
        // TODO Auto-generated method stub
        Gdx.app.debug(TAG, "shouldEndEditing");
        return true;
    }

    @Override
    public boolean shouldReturn(UITextField textField) {
        // TODO Auto-generated method stub
        if(this.mTextField == textField)
        {
            textField.resignFirstResponder();
        }
        Gdx.app.debug(TAG, "shouldReturn");
        return true;
    }
}

on ios simulator,when I click the textfield, the app will crash, and there is no error message on the console. how to solve it? Any information will be appreciate! thanks in advance!!

Was it helpful?

Solution

Your Delegate instance has probably been GCed before it is called. Try this:

Delegate delegate = new Delegate(textField);
this.textField.setDelegate(delegate);
this.textField.addStrongRef(delegate);

The addStrongRef(...) call prevents the Delegate Java instance from being GCed until the UITextField Objective-C instance is deallocated. This is required since UITextField does not retain (increase the reference count of) the instance you set as delegate.

In a future version of RoboVM the addStrongRef(...) call will be done automatically for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top