BaseViewController.m 3.7 KB
//
//  BaseViewController.m
//  ZXBaseProject
//
//  Created by 张旭 on 2017/1/10.
//  Copyright © 2017年 cnlive. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

- (id)init {
    self = [super init];
    if (self) {

        [self initParameters];
        self.isModalButton = NO;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;

    [self navigationSet];
    [self bindModel];
    [self bindView];
    [self bindAction];
}

- (void)initParameters {}

/**
 *  导航按钮设置
 */
- (void)navigationSet{
    
    self.btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
    [_btnRight setFrame:CGRectMake(0, 0, 25, 20)];
    [_btnRight addTarget:self action:@selector(onRightAction) forControlEvents:UIControlEventTouchUpInside];
    
    _btnRight.titleLabel.textAlignment = NSTextAlignmentRight;
    _btnRight.titleLabel.font = [UIFont systemFontOfSize:15];
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithCustomView:_btnRight];
    self.navigationItem.rightBarButtonItem = rightBtn;
    self.btnRight.hidden = YES;
    
    self.btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    [_btnLeft setFrame:CGRectMake(0, 0, 30, 30)];
    [_btnLeft addTarget:self action:@selector(onBackAction) forControlEvents:UIControlEventTouchUpInside];
    
    [_btnLeft setBackgroundImage:[UIImage imageNamed:@"fanhui_default"] forState:UIControlStateNormal];
    [_btnLeft setBackgroundImage:[UIImage imageNamed:@"fanhui_focused"] forState:UIControlStateHighlighted];
    _btnLeft.highlighted = NO;
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:_btnLeft];
    
    UIBarButtonItem * negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = -8;//默认leftItemX == 18,要求距左边10
    [self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,backItem, nil]];
    
    if ((self.navigationController.viewControllers[0] == self) && (self.isModalButton == NO)) {
        _btnLeft.hidden = YES;
    }
}

/**
 *  加载视图
 */
- (void)bindView {
}

/**
 *  加载模型
 */
- (void)bindModel {
}

/**
 *  加载方法
 */
- (void)bindAction {
}

- (void)loadViews {
    
}

//*****************************************************************
// MARK: - actions
//*****************************************************************

/**
 *  导航控制器上角按钮方法
 */
- (void)onBackAction {
    
    if(self.isModalButton) {
        [self dismissViewControllerAnimated:YES completion:NULL];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)onRightAction
{
}

/** 词典转换为字符串 */
- (NSString*)dictionaryToJson:(NSDictionary *)dic {
    
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end