iOS开发之基类BaseViewController和BaseTableViewController应该做哪些事情

前言

对于所有开发者而言,使用最少的代码实现必须的功能的前提下,写得一手漂亮整洁的代码是多么有成就感和美观感。那应该如何实现呢?
1、复用布局相似的view、cell、viewController;
2、封装经常使用的工具类;
3、抽取重复使用的方法;
4、创建一个很好地基类(父类)Base类;
等等.....

BaseViewController应该做哪些事情呢?

.h文件:

//左右导航按钮回调

typedef void(^PassAction)(UIButton *button);


//刷新按钮回调

typedef void(^RefreshAction)();


//传值回调

typedef void(^PassStr)(NSString *);


@interface FDBaseViewController : UIViewController


@property (copy, nonatomic) PassAction leftItemAction;

@property (copy, nonatomic) PassAction rightItemAction;

@property (copy, nonatomic) RefreshAction refreshAction;

@property (copy, nonatomic) PassStr passStr;



- (void)setNavigationBarTitle:(NSString *)title;


/**

 *  设置navigation背景透明

 */

- (void)setNavigationBackGroudDiaphanous;


/**

 *  设置navigation背景不透明

 */

- (void)setNavigationBackGroudOpaque;


#pragma mark ===== leftBarButton


/**

 *  设置navigation左按钮(图片)

 *

 *  @param image     normal图片

 *  @param highImage 高亮图片

 */

- (void)setNavigationBarLeftItemimage:(NSString *)image highImage:(NSString *)highImage;


/**

 *  设置navigation左按钮(文字)

 *

 *  @param title 按钮文字

 */

- (void)setNavigationBarLeftItemButttonTitle:(NSString *)title;


/**

 *  设置navigation左按钮(图片 + 文字)

 *

 *  @param title 文字

 *  @param image 图片

 */

- (void)setNavigationBarLeftItemButtonTitle:(NSString *)title image:(NSString *)image;


#pragma mark ===== rightBarButton

/**

 *  设置navigation右按钮

 *

 *  @param image     正常图片

 *  @param highImage 高亮图片

 */

- (void)setNavigationBarRightItemimage:(NSString *)image highImage:(NSString *)highImage;

/**

 *  设置navigation右按钮(文字)

 *

 *  @param title 按钮文字

 */

- (void)setNavigationBarRightItemButttonTitle:(NSString *)title;


/**

 *  设置navigation右按钮(图片 + 文字)

 *

 *  @param title 文字

 *  @param image 图片

 */

- (void)setNavigationBarRightItemButtonTitle:(NSString *)title image:(NSString *)image;



BaseTableViewController应该做哪些事情呢?

.m文件:

- (void)loadView

{

    [super loadView];

    self.baseTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];

    self.view = self.baseTableView;

}


- (void)dealloc

{

    self.baseTableView.emptyDataSetSource = nil;

    self.baseTableView.emptyDataSetDelegate = nil;

    self.baseTableView.delegate = nil;

    self.baseTableView.dataSource = nil;

}


- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.baseTableView.delegate = self;

    self.baseTableView.dataSource = self;

    

    self.baseTableView.emptyDataSetSource = self;

    self.baseTableView.emptyDataSetDelegate = self;

    //自适应

    self.baseTableView.estimatedRowHeight = 1000;

    self.baseTableView.rowHeight = UITableViewAutomaticDimension;

    

    [self.baseTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

    

    // Uncomment the following line to preserve selection between presentations.

    // self.clearsSelectionOnViewWillAppear = NO;

    

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.

    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

}

//公共方法----配置统一的样式

- (void)confignBaseTableView

{

    self.baseTableView.sectionFooterHeight = 0;

    self.baseTableView.sectionHeaderHeight = 20;

    self.baseTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, 20.01f)];

}



你可能感兴趣的:(iOS开发)