Objective c 关于使用Objective C在iPhone中更新视图

Objective c 关于使用Objective C在iPhone中更新视图,objective-c,iphone-sdk-3.0,core-graphics,Objective C,Iphone Sdk 3.0,Core Graphics,我有一个场景,叫做testScene,它是这样工作的: @interface testScene : myScene { IBOutlet UIView *subview; IBOutlet UIView *drawingCanvasView; IBOutlet UIButton *update; } - (void)updateDrawingCanvas: (id) sender; #import <UIKit/UIKit.h> @interface DrawingC

我有一个场景,叫做testScene,它是这样工作的:

@interface testScene : myScene {
 IBOutlet UIView *subview;
 IBOutlet UIView *drawingCanvasView;
 IBOutlet UIButton *update;
}

- (void)updateDrawingCanvas: (id) sender;
#import <UIKit/UIKit.h>


@interface DrawingCanvasView : UIView {
 CGImageRef image;

}

-(void)setNeedsDisplayInRect:(CGContextRef)context;

@end
 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextSetLineWidth(context, 2.0); 
 CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 
 CGContextMoveToPoint(context, 0.0f, 0.0f);
 CGContextAddLineToPoint(context, 100.0f, 100.0f); 
 CGContextStrokePath(context);
- (void)updateDrawingCanvas: (id) sender{
 NSLog(@"loaded");
 [DrawingCanvasView setNeedsDisplayInRect:UIGraphicsGetCurrentContext()];
}
当用户单击update按钮时,它将运行updateDrawingCanvas方法。 我有一个drawingCanvasView,它给出了drawingCanvas.h和.m,它是这样的:

@interface testScene : myScene {
 IBOutlet UIView *subview;
 IBOutlet UIView *drawingCanvasView;
 IBOutlet UIButton *update;
}

- (void)updateDrawingCanvas: (id) sender;
#import <UIKit/UIKit.h>


@interface DrawingCanvasView : UIView {
 CGImageRef image;

}

-(void)setNeedsDisplayInRect:(CGContextRef)context;

@end
 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextSetLineWidth(context, 2.0); 
 CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 
 CGContextMoveToPoint(context, 0.0f, 0.0f);
 CGContextAddLineToPoint(context, 100.0f, 100.0f); 
 CGContextStrokePath(context);
- (void)updateDrawingCanvas: (id) sender{
 NSLog(@"loaded");
 [DrawingCanvasView setNeedsDisplayInRect:UIGraphicsGetCurrentContext()];
}
我希望用户单击按钮并执行此操作,因此我添加了一个名为setNeedsDisplayInRect的新方法:

 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextSetLineWidth(context, 2.0); 
 CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor); 
 CGContextMoveToPoint(context, 0.0f, 0.0f);
 CGContextAddLineToPoint(context, 200.0f, 200.0f); 
 CGContextStrokePath(context);
但我不能在updateDrawingCanvas方法中调用它,它的工作方式如下:

@interface testScene : myScene {
 IBOutlet UIView *subview;
 IBOutlet UIView *drawingCanvasView;
 IBOutlet UIButton *update;
}

- (void)updateDrawingCanvas: (id) sender;
#import <UIKit/UIKit.h>


@interface DrawingCanvasView : UIView {
 CGImageRef image;

}

-(void)setNeedsDisplayInRect:(CGContextRef)context;

@end
 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextSetLineWidth(context, 2.0); 
 CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 
 CGContextMoveToPoint(context, 0.0f, 0.0f);
 CGContextAddLineToPoint(context, 100.0f, 100.0f); 
 CGContextStrokePath(context);
- (void)updateDrawingCanvas: (id) sender{
 NSLog(@"loaded");
 [DrawingCanvasView setNeedsDisplayInRect:UIGraphicsGetCurrentContext()];
}

这是我的逻辑/概念,对吗?或者我做错了什么,谢谢。

不,您没有覆盖
设置需要显示内容:
。您在
drawRect:
中实现绘图代码,当您调用
setNeedsDisplayInRect:
时,框架将确保调用
drawRect:

否,您不会覆盖
setNeedsDisplayInRect:
。在
drawRect:
中实现绘图代码,当调用
setNeedsDisplayInRect:
时,框架将确保调用
drawRect:

我可以drawRect:,但我无法更新视图。。。如果我调用[DrawingCanvasView setNeedsDisplay],系统将再次调用drawRect;又一次?你说的“我会画画”是什么意思?您永远不应该调用
drawRect:
。因此,我应该尽一切努力使用drawRect:,但我不能直接调用drawRect:?这是正确的。只要调用
setNeedsDisplayInRect:
setNeedsDisplay:
,框架就会为您调用
drawRect:
。苹果为什么要这样设计UIView?我可以drawRect:,但我无法更新视图。。。如果我调用[DrawingCanvasView setNeedsDisplay],系统将再次调用drawRect;又一次?你说的“我会画画”是什么意思?您永远不应该调用
drawRect:
。因此,我应该尽一切努力使用drawRect:,但我不能直接调用drawRect:?这是正确的。只需调用
setNeedsDisplayInRect:
setNeedsDisplay:
,框架就会为您调用
drawRect:
。为什么苹果会以这种方式设计UIView?