随笔 - 27, 文章 - 0, 评论 - 29, 阅读 - 51254
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

Viewcontroller基类

Posted on   吕霖  阅读(447)  评论(0编辑  收藏  举报

#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

 

(评论功能已被禁用)
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示