[转载]initwithcoder和 initwithframe

大前提是UIViewController有一个UIView.同时,需要厘清两个概念,创建一个类和实例化一个类.在XCode中创建一个类和实例化一个类很容易区分,但是在IB(Interface Builder)中有时候就会迷糊.其实也很好区分,孤零零地创建了一个nib文件,没有和其他可被实例化的类有直接或间接关系的时候,这个类或这些类(一个nib文件俺也可能包含多个类)是没有机会被实例化的,所以这种情况只是通过ib创建了一个类,而没有实例化.真正的实例化还需要通过在Xcode用代码来读取这个nib文件.知道这两这的区别后这些方法也就容易辨认多了
viewDidLoad其实没什么可混淆的,无论通过什么途径加载(Xcode或者IB,这里的加载属于实例化)完view后肯定会执行这个方法.
loadView需要分两种情况.当你通过Xcode实例化一个类的时候就需要自己在controller中实现这个方法.而在IB中实例化就不需要实现它.
initWithNibName这个方法是在controller的类在IB中创建,但是通过Xcode实例化controller的时候用的.
awakeFromNib这个方法是一个类在IB中被实例化是被调用的.看了帖子发现大家都推荐使用viewDidLoad而不要使用awakeFromNib,应为viewDidLoad会被多次调用,而awakeFromNib只会当从nib文件中unarchive的时候才会被调用一次.实际测试中发现,当一个类的awakeFromNib被调用的时候,那么这个类的viewDidLoad就不会被调用了,这个感觉很奇怪.
initWithCoder是一个类在IB中创建但在xocdde中被实例化时被调用的.比如,通过IB创建一个controller的nib文件,然后在xocde中通过initWithNibName来实例化这个controller,那么这个controller的initWithCoder会被调用.

如果你的对象是UIViewControler的子类,那么你必须调用- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil;方法去调用NIB文件初始化自身,即使那没有使用nib文件也会调用这个函数(默认情况下init方法已经为你的做这件事情了),如果你调用这个方法,并传递的两个参数为空(nil),然后类会调用-loadView去读取一个名字和你的UIViewController名字相同的nib文件,来初始化自身。如果没有这样的nib文件,你必须调用-setView:来设置一个self.view。或者重载-loadView 方法。

转自:http://blog.sina.com.cn/s/blog_5eadd29401011hzj.html

转载于:https://www.cnblogs.com/741162830qq/p/4620534.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Cocoa 绘制动态折线图的代码示例,包含 .h 和 .m 文件: DynamicLineChart.h: ```objective-c #import <Cocoa/Cocoa.h> @interface DynamicLineChart : NSView @property (nonatomic, strong) NSColor *lineColor; // 折线颜色 @property (nonatomic, assign) CGFloat lineWidth; // 折线宽度 @property (nonatomic, assign) CGFloat pointRadius; // 数据点半径 @property (nonatomic, assign) NSTimeInterval animationDuration; // 动画时长 - (void)addDataPoint:(CGFloat)dataPoint; // 添加一个数据点 @end ``` DynamicLineChart.m: ```objective-c #import "DynamicLineChart.h" @interface DynamicLineChart() @property (nonatomic, strong) NSMutableArray *dataPoints; // 存储数据点的数组 @property (nonatomic, assign) CGFloat maxValue; // 数据最大值 @property (nonatomic, assign) CGFloat minValue; // 数据最小值 @property (nonatomic, assign) CGFloat valueRange; // 数据值范围 @end @implementation DynamicLineChart - (instancetype)initWithFrame:(NSRect)frameRect { if (self = [super initWithFrame:frameRect]) { [self setup]; } return self; } - (instancetype)initWithCoder:(NSCoder *)coder { if (self = [super initWithCoder:coder]) { [self setup]; } return self; } - (void)setup { self.dataPoints = [NSMutableArray array]; self.lineColor = [NSColor blackColor]; self.lineWidth = 2.0; self.pointRadius = 3.0; self.animationDuration = 0.5; } - (void)addDataPoint:(CGFloat)dataPoint { // 将数据点添加到数组中 [self.dataPoints addObject:@(dataPoint)]; // 更新最大值和最小值 if (self.dataPoints.count == 1) { self.maxValue = dataPoint; self.minValue = dataPoint; } else { self.maxValue = MAX(dataPoint, self.maxValue); self.minValue = MIN(dataPoint, self.minValue); } self.valueRange = self.maxValue - self.minValue; // 刷新视图 [self setNeedsDisplay:YES]; } - (void)drawRect:(NSRect)dirtyRect { [super drawRect:dirtyRect]; if (self.dataPoints.count == 0) { return; } // 绘制折线 NSBezierPath *path = [[NSBezierPath alloc] init]; [[self.lineColor colorWithAlphaComponent:0.8] set]; path.lineWidth = self.lineWidth; CGFloat xInterval = self.bounds.size.width / (self.dataPoints.count - 1); for (NSInteger i = 0; i < self.dataPoints.count; i++) { CGFloat dataPoint = [self.dataPoints[i] floatValue]; CGFloat y = (dataPoint - self.minValue) / self.valueRange * self.bounds.size.height; CGFloat x = i * xInterval; if (i == 0) { [path moveToPoint:CGPointMake(x, y)]; } else { [path lineToPoint:CGPointMake(x, y)]; } } [path stroke]; // 绘制数据点 for (NSInteger i = 0; i < self.dataPoints.count; i++) { CGFloat dataPoint = [self.dataPoints[i] floatValue]; CGFloat y = (dataPoint - self.minValue) / self.valueRange * self.bounds.size.height; CGFloat x = i * xInterval; NSRect pointRect = NSMakeRect(x - self.pointRadius, y - self.pointRadius, self.pointRadius * 2, self.pointRadius * 2); NSBezierPath *pointPath = [NSBezierPath bezierPathWithOvalInRect:pointRect]; [[self.lineColor colorWithAlphaComponent:0.8] set]; [pointPath fill]; } } - (void)animate { if (self.dataPoints.count == 0) { return; } // 将视图高度缩小到 0 NSRect startRect = self.bounds; NSRect endRect = NSMakeRect(startRect.origin.x, startRect.origin.y, startRect.size.width, 0); CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"bounds"]; animation1.fromValue = [NSValue valueWithRect:startRect]; animation1.toValue = [NSValue valueWithRect:endRect]; animation1.duration = self.animationDuration / 2; animation1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; animation1.removedOnCompletion = NO; animation1.fillMode = kCAFillModeForwards; [self.layer addAnimation:animation1 forKey:@"animation1"]; // 更新数据并刷新视图 CGFloat lastDataPoint = [[self.dataPoints lastObject] floatValue]; [self.dataPoints removeAllObjects]; [self addDataPoint:lastDataPoint]; // 将视图高度恢复 CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"bounds"]; animation2.fromValue = [NSValue valueWithRect:endRect]; animation2.toValue = [NSValue valueWithRect:startRect]; animation2.duration = self.animationDuration / 2; animation2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; animation2.beginTime = CACurrentMediaTime() + self.animationDuration / 2; animation2.removedOnCompletion = NO; animation2.fillMode = kCAFillModeForwards; [self.layer addAnimation:animation2 forKey:@"animation2"]; } @end ``` 使用时,可以创建一个 DynamicLineChart 的实例,并调用 addDataPoint 方法添加数据点。如果需要动态更新折线图,可以调用 animate 方法实现动画效果。例如: ```objective-c DynamicLineChart *lineChart = [[DynamicLineChart alloc] initWithFrame:CGRectMake(0, 0, 300, 200)]; [lineChart addDataPoint:10.0]; [lineChart addDataPoint:20.0]; [lineChart addDataPoint:30.0]; [lineChart animate]; ``` 这里只是一个简单的示例,具体实现还需要根据实际需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值