开发者

How to call a method based from a protocol?

开发者 https://www.devze.com 2023-01-17 07:19 出处:网络
newbie here.This is an iPhone utility project. First things first.I have a protocol that is this: @protocol FlipsideViewControllerDelegate

newbie here. This is an iPhone utility project.

First things first. I have a protocol that is this:

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

I implement this protocol开发者_JAVA百科 in my MainViewController by doing this:

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

Now I'm wanting to use the updateLabels method in my FlipsideViewController in a method called buttonClick. How would I refer to the updateLabels method located in MainViewController?


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

By the way, while it's possible to do an unnamed parameter to a method (like you have), it's generally considered bad practice without a very good reason otherwise. :)


Based on your EDIT above, I think you might be confused about delegate protocols.

Delegation is where you have a second object, the delegate, that adopts the delegate protocol. Then the FlipsideViewController object calls methods (which are part of that protocol) on the delegate object. What this means is that FlipsideViewController shouldn't implement the FlipsideViewControllerDelegate protocol, and so you shouldn't be calling methods from that protocol on it.

Here's some more information on delegation: http://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html%23//apple_ref/doc/uid/TP40008195-CH14-SW1

And some more on protocols: http://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/Protocol.html%23//apple_ref/doc/uid/TP40008195-CH45-SW1

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号