आईओएस 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);
, आप एक आंतरिक गुमनाम struct UIBarButtonItem
वर्ग है कि इस जानकारी का भंडार का एक उदाहरण चर है कि के लिए एक typedef बना सकते हैं और उपयोग: आप आसानी से मुख्य मान कोडिंग के साथ उस तक पहुँच सकते निजी इवर का नाम, और उद्देश्य सी रन टाइम, जिन्हें आप नीचे:
//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 :(");
}
किसी भी तरह से आप इसे पहुंचते हैं, तो यह संभव है कि यह बदल जाएगा है, तो मैं शायद अपने परीक्षण केवल के संस्करणों पर सशर्त चलाने होने सुझाव देंगे ओएस इन दृष्टिकोणों में से किसी एक का समर्थन करने के लिए सत्यापित किया गया है।
स्रोत
2013-03-25 01:32:05
आप किस दावे का ढांचा उपयोग करते हैं? –