2011-05-18 91 views
2

我的Objective-C Mac應用程序中有一個文本字段。在結束編輯時執行操作

這就是我想要的:當我停止在文本字段中寫超過5秒時,它運行一些東西。這可能嗎?

如果是這樣,有人可以解釋一下嗎?

回答

3
  1. 確保您的文本字段的連續選項處於打開狀態。
  2. 將文本字段的代理連接到您的控制器。
  3. 在您的控制器中執行controlTextDidChange:
  4. 每次收到controlTextDidChange:時啓動一個計時器(並使舊計時器無效)。

下面是一個例子:

- (void)controlTextDidChange:(NSNotification *)notification 
{ 
    if (timeoutTimer != nil) { 
     [timeoutTimer invalidate]; 
     [timeoutTimer release]; 
     timeoutTimer = nil; 
    } 

    timeoutTimer = [[NSTimer 
     scheduledTimerWithTimeInterval:5.0 
     target:self 
     selector:@selector(doSomething) 
     userInfo:nil 
     repeats:NO] retain]; 
} 
+0

的文檔:「NSTextFieldDelegates的樂趣和利潤」(http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/NSTextFieldDelegate_Protocol/Reference/Reference .html),[「什麼是授權,反正?」](http://developer.apple.com/library/mac/documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html#//apple_ref/doc/uid/TP40008195-CH14-SW1)和[「我的控件的委託人整天做什麼?」](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ControlCell/Tasks /ValidatingControlEntries.html%23//apple_ref/doc/uid/20000070-BCIBIJEJ)。 – 2011-05-18 02:14:20

+0

對不起,如果我是一個完整的新手在這個,但你能指導我到一個地方,解釋如何實現東西到控制器? – objectiveccoder001 2011-05-18 02:43:32

+0

並將我的文本字段委託給控制器? – objectiveccoder001 2011-05-18 02:44:26

1

使用-performSelector:withObject:afterDelay:,如果用戶再次開始打字取消執行請求。這裏有一個最原始的例子:

- (void)controlTextDidChange: (NSNotification *)notification 
{ 
    if ([ notification object ] != myTextField) { 
     return; 
    } 

    [ NSObject cancelPreviousPerformRequestsWithTarget: self 
       selector: @selector(userStoppedEditing:) 
       object: myTextField ]; 
    [ self performSelector: @selector(userStoppedEditing:) 
      withObject: myTextField 
      afterDelay: 5.0 ]; 
} 
相關問題