便利的初始化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. tensorflow-作用域

    变量名字由两部分组成:scope/变量name. name 参数才是对象的唯一标识. 1.tf.name_scope() Graph中保存着一个属性_name_stack(string类型),_nam ...

  2. c++中的前置声明

    引用google c++编码规范: When you include a header file you introduce a dependency that will cause your cod ...

  3. Intellij Idea启用Git可视化界面

    第一步. 第二步. 然后点击OK 验证 

  4. Windows 系统采用批处理命令修改 ip 地址

    Windows 系统采用批处理命令修改 ip 地址 :: 设置IP地址 set /p choice=请选择设置类型(1:外网IP / 2:内网IP / 3:自动获取IP): echo. if &quo ...

  5. PHP与MySQL设计模式:代理模式

    一.数据库连接通用类 重要的接口: 接口用来存储MySQL连接数据.实现这个接口的类都可以使用这些数据. 通过接口可以隔离出程序中一个简单而必要的部分,任何程序都可以实现这个接口. 接口通过inter ...

  6. Python - 计算个人所得税

    最近在学python,写了个计算个人所得税计算的脚本,分享. 以下为python3适用版本 #!/usr/bin/python # -*- coding: UTF-8 -*- # 该python脚本用 ...

  7. Python3 k-邻近算法(KNN)

    # -*- coding: utf-8 -*- """ Created on Fri Dec 29 13:13:44 2017 @author: markli " ...

  8. win8预装系统环境下安装win7问题以及双操作系统安装解决

    装了许多次机器,各种操作系统,这次在win8的系统上却遇到了一些问题,现总结如下. 实验室老师给了台新DELL机器,原装的是win8操作系统,很不方便,也不想把这个系统做掉,所以就想再装个win7,即 ...

  9. 【BZOJ】3751: [NOIP2014]解方程【秦九韶公式】【大整数取模技巧】

    3751: [NOIP2014]解方程 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 4856  Solved: 983[Submit][Status ...

  10. error/exception/runtime exception区别

    (1)java中的异常是什么? 异常指的是程序运行过程中出现的非正常情况或错误,当程序违反了语义规则时,jvm就会将出现的错误表示为一个异常抛出.在java中,一切皆对象,异常也是,它被当作一个对象, ...