//button除了alloc init方法创建以外,系统也封装了类方法
+ (id)buttonWithType:(UIButtonType)buttonType;
typedef NS_ENUM(NSInteger, UIButtonType) {
//默认,如果只设置了普通背景图,没有设置高亮背景图,点击时会将普通背景图变灰
UIButtonTypeCustom = 0,
//点击时会将背景图半透明
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),
UIButtonTypeRoundedRect = UIButtonTypeSystem,
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
//这三种在ios7以后都差不多,是一个信息标记
UIButtonTypeContactAdd,
//加号标记
//2种标记都预设了图片和size大小22*22,不需要在自己设置大小和图片
};
//设置不同状态下的字体,字体颜色,背景图片
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
//状态最常用下面3个
UIControlStateNormal = 0, //默认
UIControlStateHighlighted = 1 << 0, //被点击时,(当按住按钮不放时就是这个状态)
UIControlStateSelected = 1 << 2, //选中(这个状态是得手动设置为选中或非选中)
//注:工作中高亮和选中这2个状态一般不会同时出现,如果同时设置可能引起冲突
//给按钮添加一个点击事件
//第一个参数名是目标对象(也就是给谁发消息),第二个参数是传一个方法名,(目标对象里必须有这个方法,不然就挂了)
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
//最后一个参数一般用UIControlEventTouchUpInside,点击松开时触发
//和上一个对应,删除一个点击事件
- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
tag值判断
注意:刚开始非常容易把状态的参数UIControlState和点击事件的参数UIControlEvents弄混,注意区分一个是state(状态),一个是events(事件)
//创建定时器,每隔几秒就运行某个函数一次
NSTimer *_timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(run) userInfo:nil repeats:YES];
//取消定时器
[_timer invalidate];