基类控制器(BaseViewController)

本文分享了一名iOS开发者从初级到中高级的成长经历,并提供了一些实用的代码片段,如异步线程、主线程及延时操作等方法。此外还介绍了一个自定义的基础控制器(BaseViewController),该控制器封装了常用的导航栏、跳转逻辑和背景处理等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        一个项目做了半年了,终于做完了,测试通过,提交审核,这段时间无疑是最开心的日子,小酌一杯咖啡,听段音乐,回过头来看看这段时间的日子,苦中作乐.想想从iOS初级工程师,慢慢的蜕变到中级,中高级,是一个很不容易的过程,作为一名中高级开发工程师,封装一些公用的类,基类还是很有必要的,我想现在的我也可以去写一些框架了,比如我们经常用到的基类控制器,其他的难题交给高级工程师去做吧,废话少说,直接上代码

#pragma mark - DataSource Change
//异步线程
- (void)exChangeMessageDataSourceQueue:(void (^)())queue {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
                   queue);
}
//回到主线程
- (void)exMainQueue:(void (^)())queue {
    dispatch_async(dispatch_get_main_queue(), queue);
}
//延时操作
- (void)exMainQueueTime:(NSInteger)time queue:(void (^)())queue {
    
    dispatch_after(
                   dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)),
                   dispatch_get_main_queue(), queue);
}
//====================================================
//
//  BaseViewController.h
//  ShowMe_iOS
//
//  Created by Fanjinxin on 15/11/26.
//  Copyright © 2015年 ShowMe. All rights reserved.
//

#import "PublicNavBar.h"
#import <UIKit/UIKit.h>
//如果有背景的话
typedef enum {
  BgImageTypeNavi,
  BgImageTypeNormal,
  BgImageTypeOther
} BgImageType;
@interface BaseViewController : UIViewController
/**
 *  加载StoryBoard
 */
+ (instancetype)loadStoryBoard;
/**
 *  跳转(避免循环引用)
 */
- (void)weakPushVC:(UIViewController *)VC;
/**
 *  跳转
 */
- (void)pushVC:(UIViewController *)VC;
- (void)weakPushVC:(UIViewController *)VC animated:(BOOL)animated;
- (void)pushVC:(UIViewController *)VC animated:(BOOL)animated;
- (void)weakToPopVC:(UIViewController *)VC;
- (void)popToVC:(UIViewController *)VC;
- (void)weakToPopVC:(UIViewController *)VC animated:(BOOL)animated;
- (void)popToVC:(UIViewController *)VC animated:(BOOL)animated;
/**
 * 导航栏
 */
@property(nonatomic, strong) PublicNavBar *navigationBar;

/**
 *  导航栏文字
 */
@property(nonatomic, copy) NSString *navTitle;

/**
 *  返回按钮文字
 */
@property(nonatomic, copy) NSString *backButtonTitle;

/**
 *  导航栏背景图片
 */
@property(nonatomic, copy) NSString *navBackGroundImage;

/**
 *  背景图片
 */
@property(nonatomic, strong) UIImageView *backGroundImageView;
/**
 *  背景图片类型 BgImageTypeNormal:PublicBgImage_1; BgImageTypeOther:...
 */
@property(nonatomic, assign) BgImageType bgImageType;
/**
 * 返回事件(当需点击返回时调用,默认popView)
 */
- (void)back;
/**
 * 侧滑事件(当需要右滑时调用,默认popView)
 */
- (void)handleSwipeGestureRecognizer:(UISwipeGestureRecognizer *)sgr;
/**
 * 是否允许导航控制器自带的返回效果
 */
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
/**
 *  异步线程
 */
- (void)exChangeMessageDataSourceQueue:(void (^)())queue;
/**
 *  主线程
 */
- (void)exMainQueue:(void (^)())queue;
/**
 *  延时操作
 */
- (void)exMainQueueTime:(NSInteger)time queue:(void (^)())queue;
/**
 *  设置右侧按钮是否选中
 */
- (void)setRightBtnSelected:(BOOL)selected;

- (void)goLoginVC;
@end

加载storyBoard

+ (instancetype)loadStoryBoard {
    
    NSString *vcName = [NSString stringWithFormat:@"%@", [self class]];
    if ([vcName hasSuffix:@"ViewController"]) {
        vcName = [vcName stringByReplacingOccurrencesOfString:@"ViewController"
                                                   withString:@""];
    } else if ([vcName hasSuffix:@"Controller"]) {
        vcName = [vcName stringByReplacingOccurrencesOfString:@"Controller"
                                                   withString:@""];
    }
    return [UIStoryboard storyboardWithName:vcName bundle:nil]
    .instantiateInitialViewController;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值