在适用于 iOS 的 Adob​​e Native Extension 中显示和定位 UISwitch 或任何 UIView

发布于 2024-12-28 22:51:03 字数 2298 浏览 1 评论 0原文

我正在为 iOS 编写一个简单的 Adob​​e 本机扩展。

它所做的一切都显示了一个 UISwitch。

但是,当我尝试在该 UISwitch(或这段代码中的任何 UIView)上设置Frame,甚至访问 UISwitch 的框架时,应用程序会出现段错误,导致内存访问崩溃。 UISwitch 本身不为零,因为在它上面调用 setOn 是有效的,并且如果我不尝试在它上面调用 setFrame,或者设置它的中心,或者定位它,UISwitch 确实会显示在屏幕上以任何方式。

这是代码。有问题的函数是第一个:showSwitch()

#import "FlashRuntimeExtensions.h"
#import <UIKit/UIKit.h>

FREObject showSwitch(FREContext context, 
                     void* functionData, 
                     uint32_t argc, 
                     FREObject argv[])
{
    int32_t isOn;
    FREGetObjectAsInt32(argv[0], &isOn);

    id delegate = [[UIApplication sharedApplication] delegate]; 
    UIWindow* window = [delegate window];

    // The following will segfault
    // UISwitch* switchControl = [[[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];

    // This works fine.
    UISwitch* switchControl = [[[UISwitch alloc] init] retain];

    // This will segfault as well
    // [switchControl setFrame:CGRectMake(100, 100, switchControl.frame.size.width, switchControl.frame.size.height)];

    [switchControl setOn:(isOn == 1)]; // switchControl is not nil, because this works.
    [window addSubview:switchControl];

    return NULL;
}

void ContextFinalizer(FREContext context)
{
    return;
}

void ContextInitializer(void* extensionData, 
                        const uint8_t* contextType, 
                        FREContext context, 
                        uint32_t* numFunctionsToTest, 
                        const FRENamedFunction** functionsToSet)
{
    *numFunctionsToTest = 1;

    FRENamedFunction* function = (FRENamedFunction*)malloc(sizeof(FRENamedFunction)*1);
    function[0].name = (const uint8_t*)"showSwitch";
    function[0].functionData = NULL;
    function[0].function = &showSwitch;

    *functionsToSet = function;

}

void ExtensionInitializer(void** extensionDataToSet, 
                          FREContextInitializer* contextInitializerToSet, 
                          FREContextFinalizer* contextFinalizerToSet)
{
    *extensionDataToSet = NULL;
    *contextInitializerToSet = &ContextInitializer;
    *contextFinalizerToSet = &ContextFinalizer;
}

void ExtensionFinalizer(void* extensionData)
{
    return;
}

I am writing a simple Adobe native extension for iOS.

All it does it show a UISwitch.

However, when I try to setFrame on that UISwitch (or any UIView in this piece of code), or even access the frame of the UISwitch, the app segfaults with a bad memory access crash.
The UISwitch itself is NOT nil, because calling setOn on it works, and the UISwitch does show up on the screen if I don't try to call setFrame on it, or set its center, or position it in any way.

Here's the code. The function in question is the first one: showSwitch()

#import "FlashRuntimeExtensions.h"
#import <UIKit/UIKit.h>

FREObject showSwitch(FREContext context, 
                     void* functionData, 
                     uint32_t argc, 
                     FREObject argv[])
{
    int32_t isOn;
    FREGetObjectAsInt32(argv[0], &isOn);

    id delegate = [[UIApplication sharedApplication] delegate]; 
    UIWindow* window = [delegate window];

    // The following will segfault
    // UISwitch* switchControl = [[[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];

    // This works fine.
    UISwitch* switchControl = [[[UISwitch alloc] init] retain];

    // This will segfault as well
    // [switchControl setFrame:CGRectMake(100, 100, switchControl.frame.size.width, switchControl.frame.size.height)];

    [switchControl setOn:(isOn == 1)]; // switchControl is not nil, because this works.
    [window addSubview:switchControl];

    return NULL;
}

void ContextFinalizer(FREContext context)
{
    return;
}

void ContextInitializer(void* extensionData, 
                        const uint8_t* contextType, 
                        FREContext context, 
                        uint32_t* numFunctionsToTest, 
                        const FRENamedFunction** functionsToSet)
{
    *numFunctionsToTest = 1;

    FRENamedFunction* function = (FRENamedFunction*)malloc(sizeof(FRENamedFunction)*1);
    function[0].name = (const uint8_t*)"showSwitch";
    function[0].functionData = NULL;
    function[0].function = &showSwitch;

    *functionsToSet = function;

}

void ExtensionInitializer(void** extensionDataToSet, 
                          FREContextInitializer* contextInitializerToSet, 
                          FREContextFinalizer* contextFinalizerToSet)
{
    *extensionDataToSet = NULL;
    *contextInitializerToSet = &ContextInitializer;
    *contextFinalizerToSet = &ContextFinalizer;
}

void ExtensionFinalizer(void* extensionData)
{
    return;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

早乙女 2025-01-04 22:51:03

我在向窗口添加视图时遇到了同样的问题,经过大量搜索和测试后,我找到了这个解决方案,这可能会帮助任何遇到同样问题的人。

通过这种方式添加您的子视图:

[[[[UIApplication sharedApplication] keyWindow] rootViewController].view addSubview: switchControl];

I had the same problem to add a view to the window, and after search and test a lot I found this solution, that may help anybody with the same problem.

Add your subview this way:

[[[[UIApplication sharedApplication] keyWindow] rootViewController].view addSubview: switchControl];
孤君无依 2025-01-04 22:51:03

获取 AppDelegate(不要忘记包含 .h 文件)。然后将开关添加到窗口或 rootViewController。

AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[app.window addSubview:switchControl];

Get the AppDelegate (don't forget to include the .h file). Then you add your switch to the window or to the rootViewController.

AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[app.window addSubview:switchControl];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文