开发者

My protocol method is not triggered, and no errors while trying

First time working with protocols and it is not working but no errors either...

I have defined and implemented a protocoll in a delegate (BlockPopViewController). Then I try to access it from a UIViewController (BoardViewController) whose view has been added to the delegate as a subview.

The result is that my request to the protocol's method is not creating any errors, but the method is not triggered either. Would be most appreciated if someone has an idea. Thanks in advance!

BlockPopViewController.h

#import "DirectionViewController.h"

@class BoardViewController;

@protocol BVCProtocol

- (void)testing;

@end

@interface BlockPopViewController : UIViewController <BVCProtocol> {}

-(void)testing;

@end开发者_StackOverflow中文版

BlockPopViewController.m

@implementation BlockPopViewController

-(void)testing{
    NSLog(@"Testing in delegate BlockPopViewController");   
}

@end

BoardViewController.h

@class BoardView; //This I cannot import, I think this should be ok instead. Probably cyclic import...
@class Bric;  //This I cannot import, I think this should be ok instead. Probably cyclic import...


@protocol BVCProtocol;

@interface BoardViewController : UIViewController {

}

@property(nonatomic, assign) id <BVCProtocol> blockPopViewController;

@end

BoardViewController.m

#import "BlockPopViewController.h"
#import "BoardViewController.h"

@implementation BoardViewController

@synthesize blockPopViewController;

-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event{

   NSLog(@"INSIDE TOUCHESENDED");
   [[self blockPopViewController] testing];

}


You are missing a declaration of your BlockPopViewController class which says that BlockPopViewController implements the BVCProtocol protocol.

You should also have your implementation of BVCProtocol derive NSObject in order to get proper memory management behavior. Additionally, you should derive all your protocols from the NSObject protocol in order to prevent compiler warnings when you use NSObject messages on instances of your protocol implementation.

In BlockPopViewController.h you need:

@protocol BVCProtocol <NSObject>
- (void) testing;
@end


@interface BlockPopViewController : UIViewController <BVCProtocol>
@end

In BoardViewController.h you must also #import "BlockPopViewController.h". Otherwise, the compiler won't know anything about the fact that BlockPopViewController is implementing BVCProtocol nor anything about BVCProtocol to begin with.

It would be more logical to declare a protocol in its own .h file and to import that in .h file declaring an implementation of the protocol.

You might find this little tutorial helpful.


Declare protocol outside @interface ... @end context

@protocol BVCProtocol
- (void) testing;
@end


@interface BlockPopViewController : UIViewController <BVCProtocol>
//...
@end


I think the problem I had was that I didn't create an instance of "BlockPopViewController". I changed my solution to not use protocols since it seems like overkill for me. I do have full controll of all involved classes and don't get any benefits using protocols. I do however think that this is what caused it to not work. If there is a reason for using protocols in this kind of situation which I am not aware of, pleas fill me in...


Also see Apple's Communicating with Objects, which discusses delegates, protocols, and selectors. Though its listed under Mac OS X, most (if not all) appears to apply to iOS also.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜