iOS涉及的东西很多,现在以本篇博客开始,将自己总结的一些知识点总结在此,以倒数1000的方式开始,如果有参考英文解释的,我也贴出英文原文链接,一边积累iOS基础,一边学习英文。
在某个ViewController的viewDidLoad中,
998.在父类中判断是哪个子类 2013.11.21
今天在解决问题的时候需要在父类中判断是哪个子类进行了初始化(PS:这个父类提供了一些基本的控件,并且在父类中已经写死,该父类被多个子类继承,子类的控件需要调整坐标和大小,而在子类中由于执行顺序的原因导致属性不能修改,具体的过程比较复杂不在赘述)。具体的解决方法可以抽象成下述代码所示,LeftView和RightView都继承自MainView,代码非常简单,
//LeftView.h #import "MainView.h" @interface LeftView : MainView @end //LeftView.m #import "LeftView.h" @implementation LeftView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if(self) { // Initialization code } returnself; } @end //RightView.h #import "MainView.h" @interface RightView : MainView @end //RightView.m #import "RightView.h" @implementation RightView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if(self) { // Initialization code } returnself; } @end
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. LeftView *leftView = [[LeftView alloc] initWithFrame:CGRectMake(10, 10, 150, 40)]; leftView.backgroundColor = [UIColor redColor]; [self.view addSubview:leftView]; [leftView release]; RightView *rightView = [[RightView alloc] initWithFrame:CGRectMake(160, 10, 150, 40)]; rightView.backgroundColor = [UIColor blueColor]; [self.view addSubview:rightView]; [rightView release]; }
//MainView.h #import <UIKit/UIKit.h> @interface MainView : UIView @end //MainView.m #import "MainView.h" @implementation MainView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if(self) { // Initialization code if([self isKindOfClass:[NSClassFromString(@"LeftView") class]]) { NSLog(@"Left View Init"); } elseif([self isKindOfClass:[NSClassFromString(@"RightView") class]]) { NSLog(@"Right View Init"); } } returnself; } @end