代码标记:
//MARK:视图将要消失(viewWillDisappear) 标注
//TODO: back super ViewController(返回上级视图) 备忘录
//FIXME:通常在bug的位置做该标记
#pragma message "warn one"
#warning "warn two" 这个警告是我们在项目中自己制造的,旨在引起注意
控制器:
@interface CHSViewController () <CHSViewDelegate>
@property (nonatomic, strong) CHSView *chsView;
@end
@implementation CHSViewController
#pragma mark - LifeCycle
#pragma mark-
- (void)dealloc
{
NSLog(@"界面销毁");
}
- (instancetype)init {
self = [super init ];//当前对象self
if (self !=nil) {//如果对象初始化成功,才有必要进行接下来的初始化
}
return self;//返回一个已经初始化完毕的对象;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setUI];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
#pragma mark - Public Method
#pragma mark -
#pragma mark - Private Method
#pragma mark -
- (void)setUI {
[self setMas];
}
- (void)setMas {
}
#pragma mark - IB-Action
#pragma mark -
#pragma mark - Notice
#pragma mark -
#pragma mark - Delegate
#pragma mark -
#pragma mark - lazy load
#pragma mark -
- (CHSView *)chsView {
if (!_chsView){
_chsView = [[CHSView alloc] init];
_chsView.delegate = self; //将CHSViewController自己的实例作为委托对象
}
return _chsView;
}
@end
视图:
#import <UIKit/UIKit.h>
@protocol CHSViewDelegate <NSObject>
@optional
//声明代理方法
- (void)toOperate;
@end
@interface CHSView : UIView {
}
@property (nonatomic ,weak) id <CHSViewDelegate> delegate;
@end
@interface CHSView() {
}
@end
@implementation CHSView
#pragma mark - LifeCycle
#pragma mark -
- (id)init {
self = [super init];//当前对象self
if (self !=nil) {
[self setUI];
}
return self;//返回一个已经初始化完毕的对象;
}
- (void)drawRect:(CGRect)rect {
}
#pragma mark - Public Method
#pragma mark -
#pragma mark - Private Method
#pragma mark -
- (void)setUI {
[self setMas];
}
- (void)setMas {
}
#pragma mark - IB-Action
#pragma mark -
#pragma mark - Notice
#pragma mark -
#pragma mark - Delegate
#pragma mark -
#pragma mark - lazy load
#pragma mark -
@end
Protocol(代理):
@protocol CHSViewDelegate <NSObject>
@optional
//声明代理方法
- (void)toOperate:(CHSView *)view;
@end
@property (nonatomic ,weak)id<CHSViewDelegate> delegate;
if (self.delegate && [self.delegate respondsToSelector:@selector(toOperate:)]) {
[self.delegate toOperate:self];
}
@interface CHSViewController () <CHSViewDelegate>
@end
@implementation CHSViewController
- (void)toOperate:(CHSView *)view {
}
@end
Block(闭包):
typedef void(^Block)(NSString *str);
typedef void(^CHSFetchModelDatasHandle)(NSMutableArray<CHSModel *> *models);
@property (nonatomic ,copy) Block block;
(!self.block)?:self.block(sthStr);
view.block = ^(NSString *str) {
}
- (void)fetchModelWithHandle:(CHSFetchModelDatasHandle )handle;
- (void)fetchModelWithHandle:(void(^)(NSMutableArray<CHSModel *> *models))handle;
[self fetchModelWithHandle:^(NSMutableArray<CHSModel *> * models) {
if (models.count > 0) {
}
}];
NSNotification(通知):
static NSString *const kNSNotificationKey = @"notice";
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(excuteLogic:) name: kNSNotificationKey object:nil];
- (void)excuteLogic:(NSNotification *)notification {
NSDictionary *excuteData = notification.userInfo;
NSLog(@"excuteData = %@", excuteData);
}
[[NSNotificationCenter defaultCenter] removeObserver:self name: kNSNotificationKey object:nil];
[[NSNotificationCenter defaultCenter]postNotificationName: kNSNotificationKey object:self userInfo:@{@"resCode":@0}];
NS_ENUM(枚举):
typedef NS_ENUM (NSInteger,CHSSex) {
CHSSexMan,
CHSSexWoman
}
SingleInstance(单例):
+ (instancetype)shareInstance;
static CHSUpdate *_shareInstance = nil;
+ (instancetype)shareInstance {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_shareInstance = [[super allocWithZone:NULL] init] ;
});
return _shareInstance ;
}
+ (id)allocWithZone:(struct _NSZone *)zone {
return [CHSUpdate shareInstance] ;
}
+ (id)copyWithZone:(struct _NSZone *)zone {
return [CHSUpdate shareInstance] ;
}
NSMutableDictionary(可变字典):
@property (nonatomic, strong) NSMutableDictionary *ms;
NSMutableArray(可变数组):
@property (nonatomic, strong) NSMutableArray *ms;
NSMutableArray(Model可变数组):
@property (nonatomic, strong) NSMutableArray<CHSModel *> *ms;
NSString(字符串):
@property (nonatomic, copy) NSString *str;
NSInteger(数字):
@property (nonatomic, assign) NSInteger int;
BOOL(布尔值):
@property (nonatomic, assign) BOOL bool;
Init(初始化):
- (id)init {
self = [super init ];//当前对象self
if (self !=nil) {
}
return self;//返回一个已经初始化完毕的对象;
}
自定义视图(懒加载):
- (CHSView *)chsView {
if (!_chsView){
_chsView = [[CHSView alloc] init];
_chsView.delegate = self; //将CHSViewController自己的实例作为委托对象
}
return _chsView;
}
可变视图(懒加载):
- (NSMutableArray *)ms
{
if (!_ms) {
_ms = [NSMutableArray array];
}
return _ms;
}
UILabel(懒加载):
- (UILabel *)exmapleLabel {
if(_exmapleLabel == nil) {
_exmapleLabel = [[UILabel alloc] init];
_exmapleLabel = [UIColor redColor];
_exmapleLabel =[UIFont systemFontOfSize:16];
_exmapleLabel = NSTextAlignmentLeft;
}
return _exmapleLabel;
}
UIImageView(懒加载):
- (UIImageView *)exmapleImageView {
if(_exmapleImageView == nil) {
_exmapleImageView = [[UIImageView alloc] init];
_exmapleImageView.image = [UIImage imageNamed:@""];
}
return _exmapleImageView;
}
UIButton(懒加载):
- (UIButton *)exampleButton {
if(_exampleButton == nil) {
_exampleButton = [[UIButton alloc] init];
_exampleButton.layer.cornerRadius = 2.0;
_exampleButton.tag = 0;
_exampleButton.layer.masksToBounds = YES;
_exampleButton.titleLabel.font = [UIFont systemFontOfSize:16];
[_exampleButton setTitle:@"取消关注" forState:UIControlStateNormal];
[_exampleButton setTitleColor:[UIColor color_HexStr:@"#666666"] forState:UIControlStateNormal];
[_exampleButton addTarget:self action:@selector(toDone:) forControlEvents:UIControlEventTouchUpInside];
}
return _exampleButton;
}
UITableView(懒加载):
- (UITableView *)tableView {
if(_tableView == nil) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.rowHeight = UITableViewAutomaticDimension;
_tableView.dataSource = self;
_tableView.delegate = self;
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier: NSStringFromClass([UITableViewCell class])];
//添加下拉刷新头:
_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(toPullFresh)];
//添加上拉加载尾:
_tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(toLoadMore)];
}
return _tableView;
}
UICollectionView(懒加载):
- (UICollectionView *)collectionView {
if(_collectionView == nil) {
UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
_collectionView=[[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
_collectionView.dataSource=self;
_collectionView.delegate=self;
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UITableViewCell class])];
//添加下拉刷新头:
_collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(toPullFresh)];
//添加上拉加载尾:
_collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(toLoadMore)];
}
return _collectionView;
}
UITableViewDelegate:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] init];
return headerView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *footerView = [[UIView alloc] init];
return footerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0.00001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
UITableViewDataSource:
@optional:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.ms count];
}
@require:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.ms count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *viewCell = [tableView dequeueReusableCellWithIdentifier:@"uITableViewCell"];
if (! viewCell){
viewCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([UITableViewCell class])];
}
viewCell.selectionStyle = UITableViewCellSelectionStyleNone;
return viewCell;
}
GCD(异步回到主线程):
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
});
});