iOS UIAlertView显示不出标题,及重写系统方法

最近做项目的时候,遇到一个问题,就是UIAlterView 设置了标题,但是标题一直显示不出来,但是单独出来自己写个demo,进行测试,又没问题,找了挺久的终于找到原因:

    UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"标题" message:@"这个是UIAlertView的默认样式" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"好的", nil];

    [alertview show];


新建工程后创建AlertView发现title可以正常显示,但是在我们的工程中就是无法显示title,感觉很奇怪;找了很久后发现,我们工程中给ViewController的系统setTitle方法重写了,由于重写ViewController的setTitle方法,导致AlertView 的setTitle无法调用,所以暂时我的解决方式是将Settitle方法先不重写,就可以简单粗暴的解决这个问题;

一下附上我们的重写系统setTitle方法,大家可以互相学习

 

@implementation UIViewController (Title)

+ (void)load

{

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

//        [self exchangeSystemMethod:@selector(setTitle:) customMethod:@selector(custom_setTitle:)];

    });

}

+ (void)exchangeSystemMethod:(SEL)systemMethod customMethod:(SEL)customMethod

{

    Class class = [self class];

    SEL originalSelector = systemMethod;

    SEL swizzledSelector = customMethod;

    Method originalMethod = class_getInstanceMethod(class, originalSelector);

    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);

    BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));

    if (success) {

        class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));

    } else {

        method_exchangeImplementations(originalMethod, swizzledMethod);

    }

}

- (void)custom_setTitle:(NSString *)title

{

    self.navigationItem.title = title;

}



 

你可能感兴趣的:(iOS开发,iOS控件,ios开发)