先说明一下。以前写代码时,view和viewcontroller是结合很紧密的,尤为是viewcontroller,这里面写了对view的控制和业务逻辑,还有数据的操做。一点都很差。设计模式
如今增长了一层service,用于业务逻辑和数据的操做。viewcontroller只用于view的控制和栏位的check。atom
#import <UIKit/UIKit.h> //#import "IServicebase.h" @interface UIViewController (DEcontroller) @property (nonatomic, retain) NSString *classname; @property (nonatomic,retain) id classobj; -(instancetype)initforvmobjname:(NSString *) vmname datamap:(NSDictionary *)dataDic; @end #import "UIViewController+DEcontroller.h" #import "IServicebase.h" #import <objc/runtime.h> @implementation UIViewController (DEcontroller) //@synthesize height; - (NSString *)classname { return objc_getAssociatedObject(self, @"classname"); } - (void)setClassname:(NSString *)classname { objc_setAssociatedObject(self, @"classname", classname, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (id )classobj { return objc_getAssociatedObject(self, @"classobj"); } - (void)setClassobj:(id )classobj { objc_setAssociatedObject(self, @"classobj", classobj, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } -(instancetype)initforvmobjname:(NSString *) vmname datamap:(NSDictionary *)dataDic{ UIViewController *selfobj = [self init]; Class someClass = NSClassFromString(vmname); id obj = [[someClass alloc] init]; if (!obj) { return nil; }else{ selfobj.classobj = obj; selfobj.classname = vmname; if ( [obj respondsToSelector:@selector(initclassdata:)] ){ [obj initclassdata:dataDic]; } } return selfobj; } @end
上述代码,我用到了分类。在分类中原本是不能使用属性的,只能有方法。但经过spa
#import <objc/runtime.h> - (NSString *)classname { return objc_getAssociatedObject(self, @"classname"); } - (void)setClassname:(NSString *)classname { objc_setAssociatedObject(self, @"classname", classname, OBJC_ASSOCIATION_RETAIN_NONATOMIC); }
这种作法来实现。设计
我把UIviewcontroller进行了扩展。每一个viewcontroller都有一个依赖service。code
#import <UIKit/UIKit.h> @interface IServicebase:NSObject -(void) initclassdata:(NSDictionary *)dataDic; @end #import <Foundation/Foundation.h> #import "IServicebase.h" @implementation IServicebase -(void) initclassdata:(NSDictionary *)dataDic{ NSLog(@">>>>>>>>>>>>>>>abc<<<<<<<<<<<<<<<<"); } @end
上述就是service,依赖于viewcontroller。这样的设计模式就把软件分层,各层耦合度下降。ci
view----viewcontroller----service----model,大体四层结构。get
目前仍是初步实现,有高人看到欢迎指点。it