2010-04-06 68 views
0

我有一个视图,它有一个drawRect方法,我知道这个方法是我控制视图来绘制它的唯一方法。所以,我尝试以这种方式绘制逻辑:关于在iPhone上使用石英2D绘图

- (void)drawRect:(CGRect)rect { 

    //my drawing code... 
} 

在我看来,我用IB来连接这个类。

[myView setNeedsDisplay]; 

它的工作原理,所以,我设计有drawRect方法中Command对象,这样我可以在绘制动态根据我Cmd的。下面是在查看代码我修改后:

- (void)drawRect:(CGRect)rect { 
    self.cmdToBeExecuted = [[DrawingSomethingCmd alloc] init]; 
    [self.cmdToBeExecuted execute]; 
} 

我DrawingsomthingCmd:

@implementation DrawingSomethingCmd 
-(void)execute{ 
//my drawing code; 
} 

它也能工作。但问题是,我怎样才能动态地分配self.cmdToBeExecuted。 另外,我改变了我的drawRect这样的:

- (void)drawRect:(CGRect)rect { 
    [self.cmdToBeExecuted execute]; 
    } 

因为我有这个与IB链接,

IBOutlet myDrawingView *myView; 

但在我键入[MyView的......],就不要不允许我获取变量cmdToBeExecuted。我准备好了吗使.H我的变量访问:

@property (nonatomic, retain) Command *cmdToBeExecuted; 

和.M也:

@synthesize cmdToBeExecuted; 

回答

1

不要初始化绘制矩形里面的命令。在创建视图的时候初始化一个默认的视图(可能在viewDidLoad中,它取决于你在做什么),然后动态地更新它们,但是只要需要,你就会这样做。所以:

- (void)drawRect:(CGRect)rect 
{ 
    [[self commandToBeExecuted] execute]; 
} 

和其他地方:

// dynamically update the drawing 
[myView setCommandToBeExected:[[[DrawingSomethingCommand alloc] init] autorelease]];