便利的初始化view以及设置tag值

效果

源码

https://github.com/YouXianMing/iOS-Project-Examples 中的 SetRect

//
// AccessViewTagProtocol.h
// Animations
//
// Created by YouXianMing on 16/6/17.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> typedef void(^ViewSetupBlock)(UIView * view); @protocol AccessViewTagProtocol <NSObject> @required /**
* Set the view whose tag matches the specified value.
*
* @param view View.
* @param tag tag.
*/
- (void)setView:(UIView *)view withTag:(NSInteger)tag; /**
* Remove the tag.
*
* @param tag View's tag.
*/
- (void)removeReferenceWithTag:(NSInteger)tag; /**
* Get the view from the tag.
*
* @param tag.
*
* @return view's object.
*/
- (id)viewWithTag:(NSInteger)tag; @end
//
// UIView+FrameAndTag.h
// SetRect
//
// Created by YouXianMing on 16/6/19.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h>
#import "AccessViewTagProtocol.h" @interface UIView (FrameAndTag) <AccessViewTagProtocol> #pragma mark - Set tags. /**
* Support AccessViewTagProtocol.
*/
- (void)supportAccessViewTagProtocol; /**
* Get the view with specified tag from CustomViewController type's controller.
*
* @param tag View's tag.
* @param object AccessViewTagProtocol's object.
*
* @return The view.
*/
+ (instancetype)viewWithTag:(NSInteger)tag from:(id <AccessViewTagProtocol>)object; /**
* Set the view's tag.
*
* @param tag View's tag.
* @param object AccessViewTagProtocol's object.
*/
- (void)setTag:(NSInteger)tag attachedTo:(id <AccessViewTagProtocol>)object; #pragma mark - Init frames. /**
* 设置尺寸以及设置tag值
*/
+ (instancetype)viewWithFrame:(CGRect)frame insertIntoView:(UIView *)view tag:(NSInteger)tag
attachedTo:(id <AccessViewTagProtocol>)object setupBlock:(ViewSetupBlock)block; /**
* 设置尺寸
*/
+ (instancetype)viewWithFrame:(CGRect)frame insertIntoView:(UIView *)view setupBlock:(ViewSetupBlock)block; #pragma mark - Init line view. /**
* 水平线条
*/
+ (instancetype)lineViewInsertIntoView:(UIView *)view positionY:(CGFloat)positionY thick:(CGFloat)thick
leftGap:(CGFloat)leftGap rightGap:(CGFloat)rightGap color:(UIColor *)color; /**
* 垂直线条
*/
+ (instancetype)lineViewInsertIntoView:(UIView *)view positionX:(CGFloat)positionX thick:(CGFloat)thick
topGap:(CGFloat)topGap bottomGap:(CGFloat)bottomGap color:(UIColor *)color; @end NS_INLINE id viewFrom(id <AccessViewTagProtocol> object, NSInteger tag) { return [UIView viewWithTag:tag from:object];
}
//
// UIView+FrameAndTag.m
// SetRect
//
// Created by YouXianMing on 16/6/19.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import <objc/runtime.h>
#import "UIView+FrameAndTag.h" @interface UIView () @property (nonatomic, strong) NSNumber *tagNumberValue;
@property (nonatomic, strong) NSMapTable <NSString *, UIView *> *viewsWeakMap; @end @implementation UIView (FrameAndTag) - (void)supportAccessViewTagProtocol { self.viewsWeakMap = [NSMapTable strongToWeakObjectsMapTable];
} + (instancetype)viewWithTag:(NSInteger)tag from:(id <AccessViewTagProtocol>)object { return [object viewWithTag:tag];
} - (void)setTag:(NSInteger)tag attachedTo:(id <AccessViewTagProtocol>)object { self.tagNumberValue ? [object removeReferenceWithTag:self.tagNumberValue.integerValue] : ;
self.tag = tag;
self.tagNumberValue = @(tag);
[object setView:self withTag:tag];
} + (instancetype)viewWithFrame:(CGRect)frame
insertIntoView:(UIView *)view
tag:(NSInteger)tag
attachedTo:(id <AccessViewTagProtocol>)object
setupBlock:(ViewSetupBlock)block { UIView *tmpView = [[[self class] alloc] initWithFrame:frame];
[tmpView supportAccessViewTagProtocol]; view && [view isKindOfClass:[UIView class]] ? ([view addSubview:tmpView]) : ;
object && [object respondsToSelector:@selector(setView:withTag:)] ? ([tmpView setTag:tag attachedTo:object]) : ; if (block) { block(tmpView);
} return tmpView;
} + (instancetype)viewWithFrame:(CGRect)frame
insertIntoView:(UIView *)view
setupBlock:(ViewSetupBlock)block { UIView *tmpView = [[[self class] alloc] initWithFrame:frame];
[tmpView supportAccessViewTagProtocol]; view && [view isKindOfClass:[UIView class]] ? ([view addSubview:tmpView]) : ; if (block) { block(tmpView);
} return tmpView;
} + (instancetype)lineViewInsertIntoView:(UIView *)view positionY:(CGFloat)positionY thick:(CGFloat)thick
leftGap:(CGFloat)leftGap rightGap:(CGFloat)rightGap color:(UIColor *)color { UIView *tmpView = [[[self class] alloc] initWithFrame:CGRectMake(leftGap, positionY, view.frame.size.width - leftGap - rightGap, thick)];
color ? tmpView.backgroundColor = color : ;
[view addSubview:tmpView]; return tmpView;
} + (instancetype)lineViewInsertIntoView:(UIView *)view positionX:(CGFloat)positionX thick:(CGFloat)thick
topGap:(CGFloat)topGap bottomGap:(CGFloat)bottomGap color:(UIColor *)color { UIView *tmpView = [[[self class] alloc] initWithFrame:CGRectMake(positionX, topGap, thick, view.frame.size.height - topGap - bottomGap)];
color ? tmpView.backgroundColor = color : ;
[view addSubview:tmpView]; return tmpView;
} #pragma mark - Runtime property. - (void)setTagNumberValue:(NSNumber *)tagNumberValue { objc_setAssociatedObject(self, @selector(tagNumberValue), tagNumberValue, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} - (NSNumber *)tagNumberValue { return objc_getAssociatedObject(self, _cmd);
} - (void)setViewsWeakMap:(NSMapTable<NSString *,UIView *> *)viewsWeakMap { objc_setAssociatedObject(self, @selector(viewsWeakMap), viewsWeakMap, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} - (NSMapTable<NSString *,UIView *> *)viewsWeakMap { return objc_getAssociatedObject(self, _cmd);
} #pragma mark - AccessViewTagProtocol. - (void)setView:(UIView *)view withTag:(NSInteger)tag { [self.viewsWeakMap setObject:view forKey:@(tag).stringValue];
} - (id)viewWithTag:(NSInteger)tag { return [self.viewsWeakMap objectForKey:@(tag).stringValue];
} - (void)removeReferenceWithTag:(NSInteger)tag { [self.viewsWeakMap removeObjectForKey:@(tag).stringValue];
} @end

细节

需要实现协议(用NSMapTable的strongToWeakObjectsMapTable来作为存储string - view)

获取tag更为便利,不依赖于从哪一个view中获取view,而是直接从NSMapTable中获取

便利的初始化view以及设置tag值的更多相关文章

  1. View可以设置tag携带数据

    View可以设置tag携带数据.       例子             初始化:ImageView  iv_brand2              设置:iv_brand2.setTag(strB ...

  2. c#为字段设置默认值,以及构造函数初始化List对象。

    1.为字段设置默认值 /// <summary> /// 默认值 /// </summary> ; ; /// <summary> /// 页的大小 /// < ...

  3. 结构体成员管理AVClass AVOption之2AVOption,设置选项值

    AVOption用于在FFmpeg中描述结构体中的成员变量.一个AVOption可以包含名称,简短的帮助信息,取值等. 上篇文章中概括了AVClass,AVOption和目标结构体之间的关系.以AVF ...

  4. 查看特定View的默认属性值

    当我在分析focus.touch事件处理代码时发现,有些属性对代码的逻辑有非常重要的影响,比如clickable.focusable 这些属性.这时我们自然而然的想到,那么这些属性的默认值是什么呢?在 ...

  5. [前端_EasyUI]给easyui的datebox设置默认值,获取不到 的解决方法

    //给eayui datebox设置初始值 $("#ctime").datebox("setValue", function(){ var date = new ...

  6. Browser设置UA值

    SWE Browser中的OptionMenu是Controller通过onKeyDown监听KEYCODE_MENU来显示的 public boolean onKeyDown(int keyCode ...

  7. android view的setVisibility方法值的意思

    android view的setVisibility方法值的意思 有三个值 visibility  One of VISIBLE, INVISIBLE, or GONE. 常量值为0,意思是可见的 常 ...

  8. Spring读书笔记-----Spring的Bean之设置Bean值

    [java] view plaincopyprint? Java实例的属性值可以有很多种数据类型.基本类型值.字符串类型.java实例甚至其他的Bean实例.java集合.数组等.所以Spring允许 ...

  9. View Components as Tag Helpers,离在线模板编辑又进一步

    在asp.net core mvc中增加了ViewComponent(视图组件)的概念,视图组件有点类似部分视图,但是比部分视图功能更加强大,它更有点像一个控制器. 使用方法 1,定义类派生自View ...

随机推荐

  1. SQL语句导致性能问题

    前阵子,突然收到服务器的报警信息,于是上服务器找问题,我擦,top看到mysql占的%cpu高得把我吓尿了 从以上的信息看,相信大家已经可以定位到底是那个程序导致服务器CPU负载过高了,但我们要做的是 ...

  2. CCF计算机职业资格认证考试题解

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF计算机职业资格认证考试题解 CCF计算机软件能力认证(简称CCF CSP认证)是CCF计算机职业资格认证系 ...

  3. 【LOJ】#2494. 「AHOI / HNOI2018」寻宝游戏

    题面 题解 第\(i\)个数之前的符号是或那么记为0,是与就记为1,得到一个二进数x 然后按位分开考虑,如果这一行是1那么记为1,如果这一位数位0记为0,得到一个二进制数\(b_i\) 第\(N\)行 ...

  4. Codeforces 601C Kleofáš and the n-thlon 概率dp

    Kleofáš and the n-thlon 我们可以用dp算出比当前这个人得分少的概率, 然后人数乘概率就好啦. dp[ i ][ j ]表示进行了 i 轮 得分为 j 的概率, 因为每个人都是独 ...

  5. bzoj 1143

    求最长反链裸题 补充一点知识.. 链                  :    D 中的一个子集 C   满足 C 是全序集  及C中所有元素都可以比较大小 反链              :   ...

  6. CSS------如何让ul中的li分为两列甚至多列

    转载: http://blog.sina.com.cn/s/blog_7f13f92a0100rkfg.html 只需要复制ul和li中的style样式即可 如果需要自定义多少列,只需要修改li中的w ...

  7. 洛谷P4107 [HEOI2015]兔子与樱花 [贪心,DFS]

    题目传送门 兔子与樱花 题目描述 很久很久之前,森林里住着一群兔子.有一天,兔子们突然决定要去看樱花.兔子们所在森林里的樱花树很特殊.樱花树由n个树枝分叉点组成,编号从0到n-1,这n个分叉点由n-1 ...

  8. 001.Parted工具使用

    一 Parted简介 1.1 parted和fdisk 通常使用较多的磁盘管理工具为fdisk,但由于磁盘越来越廉价,且磁盘空间越来越大,而fdisk工具分区存在大小限制,只能划分小于2T的磁盘.因此 ...

  9. python删除执行路径下的空文件夹

    def rm_emp_dir(path): """ 删除指定路径下的空文件夹 :param path: 指定路径 :type path: str :return: Non ...

  10. 关于dubbo和zookeeper 注册中心

    Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载.如果不想使用Sprin ...