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;

}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值