2010-09-24 77 views
0

新手在這裏。這是一個iPhone實用程序項目。如何根據協議調用方法?

第一件事第一件事。我有一個協議,該協議是這樣的:

@protocol FlipsideViewControllerDelegate 
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller; 
- (void)updateLabels:(NSString *)text :(BOOL)isOn; 
@end 

我做這個工具在我MainViewController此協議:

- (void)updateLabels:(NSString *)text :(BOOL)isOn { 
    [nameLabel setText:text]; 
    if (isOn) 
     [onLabel setText:(@"ON")]; 
    else 
     [onLabel setText:(@"OFF")]; 
    } 

現在我想要使用updateLabels方法在我FlipsideViewController在一個名爲方法buttonClick。我將如何引用位於MainViewController中的updateLabels方法?

+0

編輯:謝謝你的答案。我想要做的是使用一個變量來代替'@「foo」'。不管我輸入什麼內容,編譯器都說FlipsideViewController可能不會響應'-updadeLabels'。我很困難。 :) – 2010-09-24 22:08:42

回答

0

根據你上面的編輯,我想你可能會對委託協議感到困惑。

委派是您有第二個對象,委託,採用委託協議。然後FlipsideViewController對象調用委託對象上的方法(它是該協議的一部分)。這意味着FlipsideViewController不應該實現FlipsideViewControllerDelegate協議,因此您不應該從該協議調用方法。

這裏有一些代表團的更多信息:與 http://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html%23//apple_ref/doc/uid/TP40008195-CH14-SW1

更多協議的一些: http://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/Protocol.html%23//apple_ref/doc/uid/TP40008195-CH45-SW1

+0

感謝您的信息,我會給予他們一個很好的閱讀。 – 2010-09-24 22:53:51

+0

編輯:對於再次發現此問題的人,我找到了一種方法來執行此操作。你只需要'self.delegate updateLabels:@「foo」「YES];' – 2010-09-29 17:04:47

2

[self updateLabels:@"foo" :YES];

順便說一句,雖然可以做一個未命名的參數的方法(如你),它通常被認爲是不好的做法,否則沒有一個很好的理由。 :)

+0

謝謝您的信息。 – 2010-09-24 22:54:23

+0

風格提醒+1。 – 2010-09-24 23:51:39