2

I'm trying to implement a protocol.

I've looked at the documentation here and I understand the concepts although I think I'm missing a few things.

I'm trying to make a view that the user taps on a filename in a table view triggering 'didSelectRowAtIndexPath' which will in turn notify the delegate that the user has selected a file (triggering didSelectFileName in the delegate) and passing the fileName. I've declared the protocol as follows;

@protocol FileList <NSObject>
- (void)didSelectFileName:(NSString *)fileName;    
@end

My questions are:

  • How do i set the 'fileName' value so that when 'didSelectFileName' is called it has the current value in it
  • How do I tell my code to trigger 'didSelectFileName' in the delegate.
zio
  • 2,145
  • 4
  • 21
  • 25

2 Answers2

3

You cannot just send a message to a protocol (nor setting values). You send the message to a class that conforms to the protocol.


When you say a class conforms to a protocol (@interface MyClass : NSObject <MyProtocol> { etc) you can safely send any messages to the class with the selectors that conform to the methods in the protocol.

So if we take your protocol for example we can have a class that can send messages to a delegate:

@interface MyClass : NSObject {
  id<FileList> _delegate;
}

@end

@implementation MyClass

- someMethod {
  NSString *fn = @"Hello.";
  [_delegate didSelectFileName:fn];
}

@end

Just make sure you implement the methods that are in your protocol in you delegate.

You don't need to redefine the methods in the interface of your delegate class.


Here are some good reads about protocols:

Community
  • 1
  • 1
1

//In table View method

- (void)tableView didSelectRowAtIndexPath....... {
UITableViewCell *cell = [tableView methodToGetCell];
if(delegate && [delegate respondsToSelector:@selector(didSelectFileName:)]){
[delegate didSelectFileName:cell.text];
}
Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256
  • When you simply cast the delegate to `id` you don't need the slower `-[ respondsToSelector:]` because you already *know* it responds to that selector because in the given protocol it's not defined as `@optional`. If you say the delegate conforms to a protocol but doesn't implement all of the protocol's required methods, it's a programming error (and the compiler will give a warning). –  Jul 20 '11 at 12:59
  • @WTP I'm agree with you but sometimes if we don't implement protocol methods compiler just gives warning only so by mistake if we forget to implement the app will be crashed but if we check using responds to selector app will not crash. That's why I write that line. By the thanks for the information – Rahul Vyas Jul 20 '11 at 13:02
  • @WTP Can you please explain this thing in a example - (void) doSomethingWithThisObject: (id) aObject ... what is the use of (id) If we use this I think we do not need to add name in interface of another class who conforms to this protocol. Am I right? please clarify like MyCLass:UIView here we don't need to add this if we pass it on the method? – Rahul Vyas Jul 20 '11 at 13:05
  • That is a method that takes an argument. That argument must be an instance of any class that conforms to the `MyProtocolName` protocol. You *should* add the protocol name in the interface of the class, because otherwise the compiler doesn't know that the object you pass to it when calling that method conforms to `MyProtocolName`, and the compiler will give a warning. –  Jul 20 '11 at 13:06