2013-03-24 33 views
11

cố gắng kiểm tra đơn vị mà tôi đã đặt đúng UIBarButtonSystemItem trên các nút điều hướng của tôi.Nhận UIBarButtonSystemMục trở lại từ UIBarButtonItem

tôi có thể nhận được phong cách trở lại nhưng dường như không thể tìm ra cách để có được những UIBarButtonSystemItem

enums đã được đặt ra cho buttons.This thất bại vì phong cách là một enum khác với

sự 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]]]; 
} 
+0

Bạn sử dụng khung xác nhận nào? –

Trả lời

16

trên iOS 6.1 ít nhất (tôi đã không kiểm tra phiên bản khác) UIBarButtonItem có một phương pháp khai báo systemItem, mà trả về giá trị thông qua vào initializer. Bạn có thể dễ dàng truy cập vào nó với giá trị khóa mã hóa:

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); 

Nếu điều đó không làm việc, bạn có thể tạo một typedef cho một struct vô danh nội bộ đó là một biến thể hiện của lớp UIBarButtonItem lưu trữ thông tin này, và sử dụng tên Ivar tư nhân, và thời gian C chạy Mục tiêu, như hình dưới đây:

//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 :("); 
} 

Dù bằng cách nào bạn tiếp cận nó, có thể nó sẽ thay đổi, vì vậy tôi có thể khuyên bạn nên có các bài kiểm tra của bạn chạy có điều kiện duy nhất trên các phiên bản hệ điều hành được xác minh để hỗ trợ một trong các cách tiếp cận này.

Các vấn đề liên quan