如何在iOS上使用Corona Enterprise原生地更改窗口颜色?

我对Corona Enterprise和Objective C都很陌生,但是我已经在XCode中成功地进行了想要的操作,而且没有使用Corona。现在,我尝试将它放入Corona项目中,但是没有成功......

我将代码简化到最少,只将窗口颜色设置为红色。发生的情况是窗口变成了红色,但在XCode仿真器上只持续了不到一秒钟。之后,它又变成了黑色......

以下是我的AppDelegate代码:

showLoading(lua_State *L)
{
    void *platformContext = CoronaLuaGetContext(L); // lua_touserdata( L, lua_upvalueindex( 1 ) );
    id<CoronaRuntime> runtime = (id<CoronaRuntime>)platformContext;
    runtime.appWindow.backgroundColor = [UIColor redColor];

    // Return 0 since this Lua function does not return any values.
    return 0;
}

- (void)willLoadMain:(id<CoronaRuntime>)runtime
{
    lua_State *L = runtime.L;

    const luaL_Reg kFunctions[] =
    {
        "showLoading", showLoading,

        NULL, NULL
    };

    luaL_register(L, "window", kFunctions);
    lua_pop(L, 1);
}

而在main.lua上我只调用了:

window.showLoading()

我做错了什么?我只需要理解这一点,才能够在窗口中显示我想要的内容。我尝试使用runtime.appViewController.view.backgroundColor,但屏幕仍然显示黑色而不是红色......

点赞
用户736007
用户736007

好的,当我等待答案时,我发现了问题所在。以下是嵌入 AppDelegate 的新代码:

showLoading( lua_State *L )
{
    void *platformContext = CoronaLuaGetContext( L );
    id<CoronaRuntime> runtime = (id<CoronaRuntime>)platformContext;
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    if (!runtime.appViewController.view)
        runtime.appViewController.view=[[UIView alloc] initWithFrame:screenRect];
    runtime.appViewController.view.backgroundColor = [UIColor redColor];

    // Return 0 since this Lua function does not return any values.
    return 0;
}

一开始,我以为 runtime.appViewController.view 已经初始化了。现在我知道它没有,但是如果我添加了任何元素到 main.lua,它将会被初始化,所以需要一个 if 为它进行初始化,如果它仍然为 nil

2013-11-26 18:27:14