你的位置:首页 > 操作系统

[操作系统]Viewcontroller基类


#import <UIKit/UIKit.h>

#import "YQZMutableArray.h"

 

@interface YQZViewController : UIViewController

 

@property (nonatomic, strong) NSString *kTitle;

 

//保存当前正在运行的http请求

@property (nonatomic, strong) YQZMutableArray* httpRequestArray;

 

- (void)addBackBtnToHomePage;

- (void)addLeftBackBtn;

- (UIButton *)addRightTextButtonWithText:(NSString *)buttonText action:(SEL)action;

- (UIButton *)addRightImageButtonWithImageName:(NSString *)imageNamed action:(SEL)action;

- (UIButton *)addLeftTextButtonWithText:(NSString *)buttonText action:(SEL)action;

- (void)back;

- (void)backToHomepage;

- (void)addMakeCall;

- (void)loadTopData;

- (void)showLogin;

- (void)cancelHttpRequest;

@end

 

.m

#import "YQZViewController.h"

#import "YQZAppDelegate.h"

#import "YQZLoginViewController.h"

#import "SDWebImageManager.h"

#import "YQZImageCache.h"

#import "AFHttpClient.h"

 

@interface YQZViewController ()

 

@end

 

@implementation YQZViewController

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self)

    {

        self.httpRequestArray = [[YQZMutableArray alloc] init];

    }

    return self;

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    if (kdSystemVersion >= 7.0f) {

        [self.navigationController.navigationBar setTranslucent:NO];

        [self.navigationController.view setBackgroundColor:kYQZBorderGrayLightColor];

        [self setExtendedLayoutIncludesOpaqueBars:YES];

        [self setAutomaticallyAdjustsScrollViewInsets:YES];

        [self setModalTransitionStyle:UIModalTransitionStyleCoverVertical];

        [self setModalPresentationStyle:UIModalPresentationFullScreen];

        [self setEdgesForExtendedLayout:UIRectEdgeAll];

        self.edgesForExtendedLayout = UIRectEdgeNone;

    }

    

//    if (kdSystemVersion < 7.0) {

//        self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,[UIFont boldSystemFontOfSize:20],UITextAttributeFont,[NSValue valueWithUIOffset:UIOffsetZero], UITextAttributeTextShadowOffset,nil];

//    } else if (kdSystemVersion >= 7.0)

//    {

//

//    }

}

 

- (void)dealloc

{

    NSLog(@"%@ has dealloc",[NSString stringWithUTF8String:object_getClassName(self)]);

}

 

- (void)loadTopData

{

    

}

 

- (void)showLogin

{

    if ([YQZLoginViewController canAutoLogin])

    {

        [self autoLogin];

    } else {

        [self manualLogin];

    }

}

 

- (void)autoLogin

{

    [YQZSetting sharedInstance].loginStatus = LoginStatusLogOut;

    YQZLoginViewController *autoLoginViewController = [[YQZLoginViewController alloc] initWithNibName:@"YQZLoginViewController" bundle:nil];

    [autoLoginViewController autoLogin];

}

 

- (void)manualLogin

{

    YQZLoginViewController *loginViewController = [[YQZLoginViewController alloc] initWithNibName:@"YQZLoginViewController" bundle:nil];

    loginViewController.delegate = self;

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:loginViewController];

    //[self.navigationController presentViewController:navigationController animated:TRUE completion:NULL];

    YQZAppDelegate* delegate = (YQZAppDelegate*)[UIApplication sharedApplication].delegate;

    [delegate.window.rootViewController presentViewController:navigationController animated:YES completion:nil];

}

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    [[[SDWebImageManager sharedManager] imageCache] clearMemory];

    [[YQZImageCache sharedInsance] clearCachedImages];

 

}

 

- (void)back

{

    [self.view endEditing:YES];

    [self cancelHttpRequest];

    [self.navigationController popViewControllerAnimated:YES];

}

 

- (void)addMakeCall

{

    UIButton *button =[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)] ;

    button.backgroundColor = [UIColor clearColor];

    [button setImage:[UIImage imageNamed:@"打电话"] forState:UIControlStateNormal];

    [button setImage:[UIImage imageNamed:@"打电话"] forState:UIControlStateHighlighted];

    button.imageEdgeInsets = kNavigationRightButtonInSet;

    [button addTarget:self action:@selector(makeCall) forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

}

 

- (void)makeCall

{

    NSString *telStr = [NSString stringWithFormat:@"tel://%@", kCompanyPhone];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:telStr]];

    [MobClick event:@"Other_CallTel"];

}

 

- (void)backToHomepage

{

    [self.navigationController popViewControllerAnimated:YES];

}

 

- (void)addLeftBackBtn

{

    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]

                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace

                                    target:nil action:nil];

    if (kdSystemVersion >= 7.f) {

        negativeSpacer.width = -10;

    }

    UIButton *button =[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)] ;

    button.backgroundColor = [UIColor clearColor];

    [button setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];

    [button setImage:[UIImage imageNamed:@"back_h"] forState:UIControlStateHighlighted];

    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

    

    UIBarButtonItem *backNavigationItem = [[UIBarButtonItem alloc] initWithCustomView:button];

 

    self.navigationItem.leftBarButtonItems = @[negativeSpacer, backNavigationItem];

}

 

- (void)addBackBtnToHomePage

{

    UIButton *button =[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)] ;

    button.backgroundColor = [UIColor clearColor];

    [button setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];

    [button setImage:[UIImage imageNamed:@"back_h"] forState:UIControlStateHighlighted];

    [button addTarget:self action:@selector(backToHomepage) forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.leftBarButtonItem = nil;

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button] ;

}

 

- (UIButton *)addRightTextButtonWithText:(NSString *)buttonText action:(SEL)action

{

    UIButton *textButton =[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 44)];

    textButton.backgroundColor = [UIColor clearColor];

    [textButton setTitle:buttonText forState:UIControlStateNormal];

    textButton.titleLabel.font = [UIFont systemFontOfSize:kNavigationButtonFontSize];

    if (buttonText.length == 4) {

        

        if (kdSystemVersion >= 7.f) {

            textButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -15);

        }

        

    }else {

        if (kdSystemVersion >= 7.f) {

            textButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -48);

        }

    }

    [textButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:textButton];

    return textButton;

}

 

- (UIButton *)addRightImageButtonWithImageName:(NSString *)imageName action:(SEL)action

{

    UIButton *imageButton;

    if (DEVICE_IS_IPHONE6Plus||DEVICE_IS_IPHONE6PlusBig) {

        imageButton =[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];

    }

    else{

        imageButton =[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

    }

    

    imageButton.backgroundColor = [UIColor clearColor];

    [imageButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];

    [imageButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateHighlighted];

    [imageButton setImageEdgeInsets:kNavigationRightButtonInSet];

    imageButton.titleLabel.font = [UIFont systemFontOfSize:kNavigationButtonFontSize];

    [imageButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageButton];

    return imageButton;

}

 

 

- (UIButton *)addLeftTextButtonWithText:(NSString *)buttonText action:(SEL)action

{

    UIButton *textButton =[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 44)];

    textButton.backgroundColor = [UIColor clearColor];

    [textButton setTitle:buttonText forState:UIControlStateNormal];

    textButton.titleLabel.font = [UIFont systemFontOfSize:kNavigationButtonFontSize];

    [textButton.titleLabel setTextAlignment:NSTextAlignmentLeft];

    if (buttonText.length == 4) {

        

        if (kdSystemVersion >= 7.f) {

            textButton.titleEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);

        }

        

    }else {

        if (kdSystemVersion >= 7.f) {

            textButton.titleEdgeInsets = UIEdgeInsetsMake(0, -48, 0, 0);

        }

    }

    [textButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.leftBarButtonItem = nil;

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:textButton];

    return textButton;

}

 

-(void)cancelHttpRequest

{

    for (id opertion in self.httpRequestArray) {

        if ([opertion isKindOfClass:[AFHTTPRequestOperation class]]) {

            [opertion cancel];

        }

    }

    [self.httpRequestArray removeAllObjects];

}

 

@end