通过分类给控制器添加加载小菊花

在分类中动态添加属性
#import "NSObject+Associate.h"
#import <objc/runtime.h>
@interface NSObject()
@property (nonatomic,strong)id object;
@end
@implementation NSObject (Associate)
static char const * objectKey;
+ (void)test{
    NSLog(@"test:%s",__func__);
}

- (void)setObject:(id)object{
    objc_setAssociatedObject(self, objectKey, object,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (id)object{
    return objc_getAssociatedObject(self, objectKey);
}
@end

知道这个后怎样用在自己的分类之中呢?
  • 给UIViewController 添加一分类

#import "UIViewController+Utils.h"
#import <objc/runtime.h>
const static char loadingViewKey;
@implementation UIViewController (Utils)


- (UIView *)loadingView {
  return objc_getAssociatedObject(self, &loadingViewKey);
}

- (void)setLoadingView:(UIView *)loadingView {
  objc_setAssociatedObject(self, &loadingViewKey, loadingView,
                           OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)showLoadingView {
  if (!self.loadingView) {
    UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc]
        initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.loadingView = loadingView;
    [self.view addSubview:self.loadingView];

   loadingView.center = CGPointMake(kScreenWidth / 2, (kScreenHeight - 20 - 44  * 2) / 2);
    [loadingView startAnimating];
  }
}

- (void)hideLoadingView {
  if (self.loadingView) {
    [self.loadingView removeFromSuperview];
  }
}
在每次需要加载菊花的时候,使用

[self showLoadingView]

[self hideLoadingView]

  • 这样就不需要每次都去创建一个加载的菊花或者说是定义一个宏命令每次去创建调用了
  • 有没有帮助到你呢?点个赞吧
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,099评论 25 709
  • 1.自定义控件 a.继承某个控件 b.重写initWithFrame方法可以设置一些它的属性 c.在layouts...
    圍繞的城阅读 8,969评论 2 4
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,401评论 30 472
  • 因你眉眼如画 怎怪我心猿意马
    硬撑qaq阅读 1,594评论 0 0
  • 事情起源于讲台上的这几个机灵鬼每天利用课间十分钟的时间火急火燎地去食堂吃碗面才赶回来上课,日复一日的气喘吁吁和...
    赵国兴阅读 4,468评论 6 0

友情链接更多精彩内容