gpt4 book ai didi

iphone - 从 UIBarButtonItem 获取 UIBarButtonSystemItem

转载 作者:技术小花猫 更新时间:2023-10-29 10:23:37 30 4
gpt4 key购买 nike

尝试对我在导航栏按钮上设置正确的 UIBarButtonSystemItem 进行单元测试。

我可以取回样式,但似乎找不到获取 UIBarButtonSystemItem 的方法

为按钮设置的枚举。这失败了,因为样式与枚举不同

UIBarButtonSystemItem:

- (void)test_init_should_set_left_right_barButtonItems {

UIBarButtonItem *left = mainVCSUT.navigationItem.leftBarButtonItem;
UIBarButtonItem *right = mainVCSUT.navigationItem.rightBarButtonItem;
[Assert isNotNil:left];
[Assert isNotNil:right];

UIBarButtonItemStyle leftStyle = left.style;
UIBarButtonItemStyle rightStyle = right.style;

[Assert that:[The int:leftStyle] is:[Equal to:[The int:UIBarButtonSystemItemRefresh]]];
[Assert that:[The int:rightStyle] is:[Equal to:[The int:UIBarButtonSystemItemSearch]]];
}

最佳答案

至少在 iOS 6.1 上(我还没有测试过其他版本)UIBarButtonItem 有一个未声明的方法 systemItem,它返回传递给初始化程序的值。您可以使用键值编码轻松访问它:

UIBarButtonSystemItem systemItemIn = UIBarButtonSystemItemAdd;
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItemIn target:nil action:NULL];
NSNumber *value = [item valueForKey:@"systemItem"];
UIBarButtonSystemItem systemItemOut = [value integerValue];
NSLog(@"systemItemIn = %d, systemItemOut = %d", systemItemIn, systemItemOut);

如果这不起作用,您可以为内部匿名结构创建一个 typedef,它是存储此信息的 UIBarButtonItem 类的实例变量,并使用私有(private) ivar 的名称和 Objective C运行时,如下图:

//Copied from UIBarButtonItem.h, this is the struct used for the _barButtomItemFlags ivar
typedef struct {
unsigned int enabled:1;
unsigned int style:3;
unsigned int isSystemItem:1;
unsigned int systemItem:7;
unsigned int viewIsCustom:1;
unsigned int isMinibarView:1;
unsigned int disableAutosizing:1;
unsigned int selected:1;
unsigned int imageHasEffects:1;
} FlagsStruct;

// In our test code
// Instantiate a bar button item
UIBarButtonSystemItem systemItemIn = UIBarButtonSystemItemAdd;
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItemIn target:nil action:NULL];

// Set up variables needed for run time functions
Class barItemClass = [item class];
BOOL foundIt = NO; // We check this flag to make sure we found the ivar we were looking for
ptrdiff_t ivarOffset = 0; // This will be the offset of _barButtomItemFlags within the bar button item object

// Iterate through all of UIBarButtonItem's instance variables
unsigned int ivarCount = 0;
Ivar *ivarList = class_copyIvarList(barItemClass, &ivarCount);
for (int i = 0; i < ivarCount; i++) {
Ivar ivar = ivarList[i];
const char *ivarName = ivar_getName(ivar);
if (!strcmp(ivarName, "_barButtonItemFlags")) {
// We've found an ivar matching the name. We'll get the offset and break from the loop
foundIt = YES;
ivarOffset = ivar_getOffset(ivar);
break;
}
}
free(ivarList);

if (foundIt) {
// Do a little pointer math to get the FlagsStruct - this struct contains the system item value.
void *itemPointer = (__bridge void *)item;
FlagsStruct *flags = itemPointer + ivarOffset;
UIBarButtonSystemItem systemItemOut = flags->systemItem;
NSLog(@"systemItemIn = %d, systemItemOut = %d", systemItemIn, systemItemOut);
BOOL equal = (systemItemIn == systemItemOut);
if (equal) {
NSLog(@"yes they are equal");
}
else {
NSLog(@"no they are not");
}
}
else {
// Perhaps Apple changed the ivar name?
NSLog(@"didn't find any such ivar :(");
}

无论采用哪种方式,它都可能会发生变化,因此我可能建议您有条件地仅在经验证支持这些方法的操作系统版本上运行测试。

关于iphone - 从 UIBarButtonItem 获取 UIBarButtonSystemItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15603375/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com