2012-02-11 86 views
0

我想获得一个按钮来创建一个NSTimer,它反过来会调用一个函数(refreshView)刷新UI元素,但我有问题,我不知道问题出在哪里。方法签名是否错误?或者我得到的NSRunLoop部分错了?或者它只是可怕的基地?任何帮助表示赞赏。NSInvocation问题NSTimer和NSMethodSignature

-(IBAction)reload:(id)sender{ 
NSInvocation *displayInvocation = [NSInvocation invocationWithMethodSignature:[self  methodSignatureForSelector:@selector(refreshView)]]; 
[displayInvocation setTarget:self]; 
NSTimer *slideShowTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 
                invocation:displayInvocation 
                  repeats:YES]; 
[slideShowTimer fire]; 
NSRunLoop * a = [NSRunLoop currentRunLoop]; 
[a addTimer:slideShowTimer forMode:NSRunLoopCommonModes];} 

-(void)refreshView{ 
[slideshow1 displayWithView:MajorImageView topicLabel:TopicLabel]; 
} 

回答

2

您的代码看起来非常复杂。你想(1)定期启动一个定时器来定期调用[refreshView],或者(2)稍后调用它。

对于(1),只需安装一个定时器,

[NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)seconds 
           target:(id)target 
           selector:(SEL)aSelector 
           userInfo:(id)userInfo 
           repeats:(BOOL)repeats] 

没必要用一个方法调用,一个目标/行动将足以

对于(2),如果你想稍后调用它,

[NSObject performSelector:(SEL)aSelector 
       withObject:(id)anArgument 
       afterDelay:(NSTimeInterval)delay] 
+0

为了清楚起见,应该提到用'scheduledTimer ...'创建的定时器已被_already_添加到运行循环中。定时器也不需要发送'fire'。 – 2012-02-11 19:03:09

+0

谢谢,它现在工作完美! – 2012-02-12 18:56:59