优化ViewController

来源:互联网 发布:mysql有哪几种连接池 编辑:程序博客网 时间:2024/06/02 03:32

ViewControllers 通常是 iOS 项目中最大的文件,如何对VC进行优化以便于管理:


(1)可以把view的datasource放到一个单独的类:such as UITableViewDataSource

eg:

@implementation ArrayDataSource

- (id)itemAtIndexPath:(NSIndexPath*)indexPath {

    return items[(NSUInteger)indexPath.row];

}

- (NSInteger)tableView:(UITableView*)tableView

 numberOfRowsInSection:(NSInteger)section {

    return items.count;

}

- (UITableViewCell*)tableView:(UITableView*)tableView

        cellForRowAtIndexPath:(NSIndexPath*)indexPath {

    id cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier

                                              forIndexPath:indexPath];

    id item = [self itemAtIndexPath:indexPath];

    configureCellBlock(cell,item);

    return cell;

}

@end


//调用

void (^configureCell)(PhotoCell*, Photo*) = ^(PhotoCell* cell, Photo* photo) {

   cell.label.text = photo.name;//给cell的subview赋值

};

photosArrayDataSource = [[ArrayDataSource alloc] initWithItems:photos

                                                cellIdentifier:PhotoCellIdentifier

                                            configureCellBlock:configureCell];

self.tableView.dataSource = photosArrayDataSource;

    

(2)将业务逻辑移到 Model 中

对model的数据处理放到model类中

eg: 拍照我的相册里面,处理相册年月日 

-(MyAlbumModel *)returnMyAlbumModel:(MyAlbumModel *)albumObj

{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"yyyy-MM-dd"];

    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai"]];

    NSDate *date = [dateFormatter dateFromString:albumObj.albumTime];


    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit |

                                    NSMonthCalendarUnit | NSYearCalendarUnit fromDate:date];


    NSUInteger year = [components year];

    albumObj.year = [NSString stringWithFormat:@"%d",year];

    NSUInteger month = [components month];

    albumObj.month = [NSString stringWithFormat:@"%d",month];

    NSUInteger day = [components day];

    albumObj.day = [NSString stringWithFormat:@"%d",day];

    

    dateFormatter = nil;date = nil;components = nil;

    return albumObj;

}

//调用

MyAlbumModel *album = (MyAlbumModel *)tempArray[0];

                    NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"year == %@ AND month == %@ AND day == %@", album.year, album.month,album.day];

                    NSMutableArray  *filteredArray = [NSMutableArray arrayWithArray:[tempArray filteredArrayUsingPredicate:predicateString]];


有些代码不能被轻松地移动到 model 对象中,但明显和 model 代码紧密联系,可以构建一个中间类

eg:拍照聊天对象包含用户和聊天内容

@interface RPContactModel : NSObject//聊天对象

@interface RPMessageModel : NSObject//消息

//构建Union对象

@interface RPMessageUserUnionModel : NSObject


@property (nonatomic,strong) RPMessageModel *messageObj;

@property (nonatomic,strong) RPContactModel *contactObj;


+(RPMessageUserUnionModel *)unionWithMessage:(RPMessageModel *)aMessage andUser:(RPContactModel *)aContact;


@end



(3)把网络请求逻辑移到 Model 层

把网络请求封装到另一个类中,这样VC就可以在之后通过使用带有回调(比如一个 completion 的 block)来请求网络了。这样的好处是,缓存和错误控制也可以在这个类里面完成。

eg:拍照的请求接口

+ (id)request:(NSMutableDictionary *)params requestUrl:(NSString *)requestUrl method:(NSString *)method completeBlock:(CompleteBlock_t)compleBlock errorBlock:(ErrorBlock_t)errorBlock;


(4)把 View 代码移到 View 层

不要再VC中构建复杂的 view 层次结构。你可以使用 Interface Builder 或者把 views 封装到一个 UIView 子类当中。


eg:拍照加载框的波浪图

@interface WaterView : UIView


@property (nonatomic, strong) UIColor *currentWaterColor;


@property (nonatomic, assign) float currentLinePointY;

@property (nonatomic, assign) float a;

@property (nonatomic, assign) float b;

 @property (nonatomic, assign) BOOL jia;


-(void)start;

-(void)stop;

@end


//调用

[self.view insertSubview:self.whiteView belowSubview:self.water];

[self.water start];


(5)多个VC方法的重用

许多方法在VC中都会调用,可以构建一个类方法

eg:拍照VC设置背景、弹出框、获取日期、加载loading图、设置圆角等

@interface Utility : NSObject


+ (NSString *)getNowDateFromatAnDate;


+ (void)roundView: (UIView *) view;


+(void)setLeftRoundcornerWithView:(UIView *)view;


+(void)setRightRoundcornerWithView:(UIView *)view;


+ (void)errorAlert:(NSString *)message dismiss:(BOOL)animated;


+(void)animationWithView:(UIView *)view image:(NSString *)image selectedImage:(NSString *)selectedImage type:(int)type;


+ (NSString*)getCurrentTimeString;


+(CGSize)returnSizeWithString:(UIImage *)image;


+(void)initAlixPayOrder:(AliPayModel *)aliPayObj;


+(void)cancelOrder:(AliPayModel *)aliPayObj;


+(NSString *)returnPath;


+(void)showLoadingVCWithParent:(UIViewController *)viewControl message:(NSString *)message;


+(void)dismissLoadingVCWithParent:(UIViewController *)viewControl;


+(BOOL)checkTitleString:(NSString *)string;


+(BOOL)checkString:(NSString *)string;


+(UIViewController *)loadVCWith:(NSString *)string;


+ (NSString*)deviceString;


+ (void)replaceContainerView:(UIView *)view topConstraintWithConstant:(CGFloat)constant;


+(void)setBackGround:(UIViewController *)VC WithImage:(NSString *)imageName;


+ (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize image:(UIImage*)aImage;

@end






0 0
原创粉丝点击