2012-02-11 26 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