My protocol method is not triggere, but not erros while trying...

Discussion in 'iOS Programming' started by Nicsoft, Dec 11, 2009.

  1. macrumors member

    Joined:
    Oct 17, 2009
    Location:
    Stockholm
    #1
    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

    Code:
    #import "DirectionViewController.h"
        
        @class BoardViewController;
        
        @protocol BVCProtocol <NSObject>
        
        - (void)testing;
        
        @end
    
        @interface BlockPopViewController : UIViewController <BVCProtocol> {}
        
        -(void)testing;
        
        @end
    BlockPopViewController.m

    Code:
     @implementation BlockPopViewController
        
        -(void)testing{
        	NSLog(@"Testing in delegate BlockPopViewController");	
        }
        
        @end

    BoardViewController.h
    #import "BlockPopViewController.h"

    Code:
    @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

    Code:
    #import "BlockPopViewController.h"
        #import "BoardViewController.h"
        
        @implementation BoardViewController
        
        @synthesize blockPopViewController;
        
        -(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event{
        
           NSLog(@"INSIDE TOUCHESENDED");
           [[self blockPopViewController] testing];
        
        }
    
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    You need to assign blockPopViewController to your actual class. Otherwise it's calling the delegate method on a nil object.
     
  3. thread starter macrumors member

    Joined:
    Oct 17, 2009
    Location:
    Stockholm
    #3
    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...
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    I don't see it.

    Just do this and see what it logs:
    Code:
    NSLog(@"blockPopViewController: %@", self.blockPopViewController);
     
  5. thread starter macrumors member

    Joined:
    Oct 17, 2009
    Location:
    Stockholm
    #5
    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?
     
  6. thread starter macrumors member

    Joined:
    Oct 17, 2009
    Location:
    Stockholm
    #6
    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.

    Code:
    -(void) loadView{
    [INDENT][[NSBundle mainBundle] loadNibNamed:@"BoardView" owner:self options:nil];
    self.view = boardView;
    [/INDENT]}
    
    The second row was unessecary and created errors, appearently.

    Thanks for helping out!
     
  7. thread starter macrumors member

    Joined:
    Oct 17, 2009
    Location:
    Stockholm
    #7
    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?
     

Share This Page