为了触发drawRect方法,我尝试在NSTimer选择器中调用self setNeedsDisplay:YES。
首先,我将NSTimer初始化代码放在一个按钮函数中:
-(IBAction)buttonPush:(id)sender
{
myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(myTimerAction:)
userInfo:nil
repeats:YES];
}
-(void)myTimerAction:(NSTimer *) timer
{
[self setNeedsDisplay:YES];
} 正常调用"setNeedsDisplay“,但从不调用drawRect中的代码:
- (void)drawRect:(NSRect)dirtyRect
{
NSLog(@"drawRect");
}然后我尝试将NSTimer初始化代码移到"- (id)initWithFrame:(NSRect)frame“中,然后一切正常。( drawRect每1秒正确调用一次)。
上面两种方法有什么不同?如果我想在按钮中触发计时器,我应该怎么做?
发布于 2011-02-05 04:19:52
我只是想知道,这些代码驻留在哪个类中?我假设buttonPush: action在控制器内部,对吗?
如果是这样,那么您应该拥有:
-(void)myTimerAction:(NSTimer *) timer
{
[[self view] setNeedsDisplay:YES];
}因为setNeedsDisplay:是NSView的方法,而不是NSViewController。
(顺便说一句,如果你把它放在initWithFrame:中,它能工作的原因可能是因为它是一个NSView初始化器:我猜当你把代码移到那里的时候,你也移动了myTimerAction:方法,然后"self“正确地引用了视图。)
发布于 2011-02-10 19:07:19
由于它在您使用initWithFrame:时工作,所以问题可能是buttonPush:没有正确连接。尝试在buttonPush中设置断点:查看在单击按钮时是否实际调用了断点。
https://stackoverflow.com/questions/4871842
复制相似问题