2012-09-19 24 views
20

Tôi đang cố gắng phát hiện phương thức xuất hiện dành riêng cho iOS 6, bằng cách chạy respondsToSelector trên [UIBarButtonItem appearance]. Tuy nhiên, nó luôn luôn trả NO đối với tôi, bất cứ điều gì selector tôi chỉ định:respondsToSelector không xuất hiện proxy

// Should show NOPE in iOS 5, YEP in iOS 6. Shows NOPE always 
NSLog(@"%@", [[UIBarButtonItem appearance] respondsToSelector:@selector(setBackgroundImage:forState:style:barMetrics:)] ? @"YEP" : @"NOPE"); 

// Should show YEP in both iOS 5 and iOS 6. Shows NOPE always 
NSLog(@"%@", [[UIBarButtonItem appearance] respondsToSelector:@selector(setBackgroundImage:forState:barMetrics:)] ? @"YEP" : @"NOPE"); 

Trên thực tế sử dụng các phương pháp đó hoạt động tốt trên các phiên bản tương ứng của họ iOS, nhưng tôi dường như không thể phát hiện cái nào là có sẵn cho tôi. Vì vậy, làm thế nào để tôi làm điều đó đúng cách?

Trả lời

36

Đừng kiểm tra proxy xuất hiện. Bạn không bao giờ có thể dựa vào đó, vì nó là một proxy. Thay vào đó, hãy kiểm tra trực tiếp mục có phương pháp mới, trong trường hợp này là UIBarButtonItem:

BOOL hasNewMethod = [UIBarButtonItem instancesRespondToSelector:@selector(setBackgroundImage:forState:style:barMetrics:)]; 
if(hasNewMethod) 
    NSLog(@"Running iOS 6 with new method"); 
else 
    NSLog(@"Current OS doesn't support method..."); 
+1

+1 Bạn Sir tuyệt vời! – bgolson

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