Custom navigationItem button with QLPreviewController in iOS6

后端 未结 5 1142
悲哀的现实
悲哀的现实 2020-12-09 12:06

my goal is to use QLPreviewController in my iPad application for iOS6, using my custom Action item button in the top toolbar. I had solution until iOS5.1. I used a class tha

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 12:49

    I really really needed a solution, so I made this up.

    Yes, it's ugly. Yes, it may break at any time. And yes, I'll go strait to dev hell, but my boss did stop stare at me with angry eyes...for now.

    @implementation UINavigationItem (Custom)
    
    void MethodSwizzle(Class c, SEL origSEL, SEL overrideSEL);
    
    - (void) override_setRightBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated{   
        if (item && [item.target isKindOfClass:[QLPreviewController class]] && item.action == @selector(actionButtonTapped:)){
            QLPreviewController* qlpc = (QLPreviewController*)item.target;
            [self override_setRightBarButtonItem:qlpc.navigationItem.rightBarButtonItem animated: animated];
        }else{
            [self override_setRightBarButtonItem:item animated: animated];
        }
    }
    
    + (void)load {
        MethodSwizzle(self, @selector(setRightBarButtonItem:animated:), @selector(override_setRightBarButtonItem:animated:));
    }
    
    void MethodSwizzle(Class c, SEL origSEL, SEL overrideSEL) {
        Method origMethod = class_getInstanceMethod(c, origSEL);
        Method overrideMethod = class_getInstanceMethod(c, overrideSEL);
    
        if (class_addMethod(c, origSEL, method_getImplementation(overrideMethod), method_getTypeEncoding(overrideMethod))) {
            class_replaceMethod(c, overrideSEL, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
        }else{
            method_exchangeImplementations(origMethod, overrideMethod);
        }
    }
    
    @end
    

    Steve Jobs will hunt me in my dreams until I find a proper solution...

提交回复
热议问题