一. 定义枚举型
//常用
typedef NS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = 0,
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd
};
//or
typedef NS_OPTIONS(NSUInteger, UIControlState) {
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0,
UIControlStateDisabled = 1 << 1,
UIControlStateSelected = 1 << 2,
UIControlStateFocused NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 3,
UIControlStateApplication = 0x00FF0000,
UIControlStateReserved = 0xFF000000
};
两者区别?
//NS_ENUM, 取值时只能取其中一个,单选题
//NS_OPTIONS, 可以使用|(或运算符),多选题
我的使用经验:
1、当你的枚举型定义在任意一个.h文件, 比如你封装的view类, 如果vc引入此.h文件,就能直接在vc中使用此枚举类型的.
2、在架构级别,可以写在专门的.h文件,并导入.pch文件中
二. 定义Block类型
typedef void(^MyBlock)(UIButtonType type, NSInteger index);
//block的触发,必须先判断self.block是否不为nil;
//正如delegate的方法触发,先判断self.delegte!=nil, 且responseTo方法.