PDA

View Full Version : My protocol method is not triggere, but not erros while trying...




Nicsoft
Dec 11, 2009, 04:20 AM
Hello,

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 <NSObject>

- (void)testing;

@end

@interface BlockPopViewController : UIViewController <BVCProtocol> {}

-(void)testing;

@end

BlockPopViewController.m

@implementation BlockPopViewController

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

@end


BoardViewController.h
#import "BlockPopViewController.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 {

id <BVCProtocol> blockPopViewController;
}

@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];

}



kainjow
Dec 11, 2009, 09:46 AM
You need to assign blockPopViewController to your actual class. Otherwise it's calling the delegate method on a nil object.

Nicsoft
Dec 12, 2009, 06:54 AM
Thanks for your answer.

Actually, I did that, that line dropped when doing the copy/pasting of the code into here. I did edit above to reflect how it looks like. Sorry for the mess...

kainjow
Dec 12, 2009, 12:55 PM
I don't see it.

Just do this and see what it logs:
NSLog(@"blockPopViewController: %@", self.blockPopViewController);

Nicsoft
Dec 13, 2009, 07:32 AM
Hmm...it does print "blockPopViewController: (null)"...

Why am I not getting an instance? I did try to make an Outlet of "id <BVCProtocol> blockPopViewController;" and link it to a an Object associated with class BlockPopViewController in IB. That gave me errors... I guess I must access the actual object that did add the subview BoardView (of BoardViewController) and has the protocol implemented, in some way, but how?

Nicsoft
Dec 14, 2009, 10:09 AM
I skipped using protocols. I did create a property in BoardViewController and a method named setBlockPopViewController which is registering the BlockPopViewController in BoardViewController. This I do from the BlockPopViewController after the BoardViewController is created. Then I can access any method I want in BlockPopViewController from BoardViewController.

Is this the prefered way of doing it? I cannot really see the benefit of using protocols, perhaps because I didn't manage to get it working... I had one problem which probably caused some problems when using protocols.


-(void) loadView{
[[NSBundle mainBundle] loadNibNamed:@"BoardView" owner:self options:nil];
self.view = boardView;
}


The second row was unessecary and created errors, appearently.

Thanks for helping out!

Nicsoft
Dec 15, 2009, 03:26 AM
You need to assign blockPopViewController to your actual class. Otherwise it's calling the delegate method on a nil object.

Ah, I think I didn't get this.

"My actual class" I assume is BoardViewController, correct? So just having a property in that class which I can assign blockPopViewController to, like this in BlockPopViewController: [boardViewController setBlockPopViewController:self];

If that is the case, I guess using protocols is overkill since I am having control over both sending and receiving class?