在适用于 iOS 的 Adobe Native Extension 中显示和定位 UISwitch 或任何 UIView
我正在为 iOS 编写一个简单的 Adobe 本机扩展。
它所做的一切都显示了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在向窗口添加视图时遇到了同样的问题,经过大量搜索和测试后,我找到了这个解决方案,这可能会帮助任何遇到同样问题的人。
通过这种方式添加您的子视图:
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:
获取 AppDelegate(不要忘记包含 .h 文件)。然后将开关添加到窗口或 rootViewController。
Get the AppDelegate (don't forget to include the .h file). Then you add your switch to the window or to the rootViewController.