How to call protocol

Discussion in 'iOS Programming' started by Yandroidpn, Nov 10, 2011.

  1. Yandroidpn macrumors newbie

    Joined:
    Nov 10, 2011
    #1
    How can I call protocol?
    Here is a code that I based on "ScrollViewSuite".
    Code:
    @protocol TestViewTouchDelegate;
    
    @interface TestView : TestGLView 
    {
        // Interaction.
        id <TestViewTouchDelegate> touchDelegate;
    }
    
    // Interaction.
    @property(nonatomic, assign) id <TestViewTouchDelegate> touchDelegate;
    
    @end
    
    @protocol TestViewTouchDelegate <NSObject>
    
    @optional
    - (void)tapDetectingView:(UIView *)view touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event;
    
    @end
    
    Code:
    #import "TestView.h"
    
    @implementation TestView
    @synthesize touchDelegate;
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [touchDelegate tapDetectingView:self touchesBegan:touches withEvent:event];
        
        // update our touch state
        if ([[event touchesForView:self] count] > 1)
            multipleTouches = YES;
        if ([[event touchesForView:self] count] > 2)
            twoFingerTapIsPossible = NO;
        NSLog(@"Test");
    }
    
    Code:
    #import "TestView.h"
    
    @interface TestObject : NSObject <TestViewTouchDelegate> {
        // Interaction.
        id <TestViewTouchDelegate> touchDelegate;
        float gDrawRotateAngle;
    }
    
    // Interaction.
    @property(nonatomic, assign) id <testViewTouchDelegate> touchDelegate;
    
    @end
    
    Code:
    #import "TestView.h"
    
    @implementation TestObject
    @synthesize touchDelegate;
    
    - (void)updateWithTimeDelta:(NSTimeInterval)timeDelta
    {
            gDrawRotateAngle += (float)timeDelta * 45.0f; // Rotate cube at 45 degrees per second.
            if (gDrawRotateAngle > 360.0f) gDrawRotateAngle -= 360.0f;
    }
    
    - (void)tapDetectingView:(UIView*)view touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
    {
        gDrawRotateAngle = !gDrawRotateAngle;
        NSLog(@"Test5");
    }
    
    I can get "Test", but I cannot get "Test5".
    Please advice what I am missing.

    Regards,
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    Get rid of the ivar declaration and always refer to touchDelegate as self.touchDelegate.

    Code:
    @interface TestView : TestGLView
    
    @property(nonatomic, assign) id <TestViewTouchDelegate> touchDelegate;
    
    @end
    
    Code:
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self.touchDelegate tapDetectingView:self touchesBegan:touches withEvent:event];
        // etc.
    }
    
    This will remove any ambiguity between the property and the ivar. Objective-C will synthesise an ivar to back to property for you.

    Also where does touchDelegate get assigned? Perhaps log the value of self.touchDelegate in touchesBegan:withEvent: to check that it's not nil.
     
  3. Yandroidpn thread starter macrumors newbie

    Joined:
    Nov 10, 2011
    #3
    Thank you for your replay. But I still cannot get "Test5". Even I comment out for other touch event. So I have touchesBegan only now.
    What I am trying to make is App using ARToolkit for iPhone.
    There is already touch event in TestView.m although this is not currently used in the application. But I want my model to make rotate, scaling and positioning somehow.

    touchDelegate is assigned in TestView.h

    Please give me some more advice.
    Regards,
     
  4. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    So I assume when you logged self.touchDelegate it was nil? You didn't say.

    No it's not. All you've done is declare the protocol and declared that TestObject conforms to that protocol. At some point to need to assign an instance of TestObject to the touchDelegate property of an instance of TestView.
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    Use NSLog to tell you something more informative than a literal "Test" string. See added code in red.
     
  6. Yandroidpn thread starter macrumors newbie

    Joined:
    Nov 10, 2011
    #6
    How can I check it?
    I just started to learn Xcode and ARToolkit for three week age, even I have non knowledge of C++. I know I am doing reckless task. But I have to finish it ASAP. Please help.

    Then I changed my code as attach. Unfortunately, it still not working.

    Regards,
     

    Attached Files:

  7. Yandroidpn thread starter macrumors newbie

    Joined:
    Nov 10, 2011
    #7
    I got this.
    touchDelegate is: (null)

    But I still cannot get Test5...
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    And you won't, until you actually set a delegate in an instance of TestView.

    You don't seem to be thinking this through logically. You have this code in TestView's implementation:
    Code:
    @implementation TestView
    @synthesize touchDelegate;
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog( @"touchDelegate is: %@", self.touchDelegate );
    
        [touchDelegate tapDetectingView:self touchesBegan:touches withEvent:event];
    
    Take it piece by piece:
    Code:
    @implementation TestView
    @synthesize touchDelegate;
    
    This says that instances of the TestView class have a touchDelegate property.

    Code:
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    
    This says that instances of the TestView class have that method.

    Code:
        NSLog( @"touchDelegate is: %@", self.touchDelegate );
    
    When this code runs, and it logs "touchDelegate is: (null)", it means that the touchDelegate is null. If the delegate is null, it means no delegate has been assigned to the TestView object instance.

    Connections between objects don't happen by magic. Declaring a variable or property doesn't automatically make a connection to other object of that type. If you don't set the touchDelegate property of your TestView instance to something, then that property will remain nil.

    Code:
        [touchDelegate tapDetectingView:self touchesBegan:touches withEvent:event];
    
    This sends a message (tapDetectingView:self) to the touchDelegate object if there is one. If touchDelegate is nil, however, then you're sending the message to nil, so nothing happens.

    Since you aren't making a connection between a TestView object and a touchDelegate object, there's no way you will ever see the "Test5" log message. If you don't understand why that happens, then you don't understand the connections between your objects, or the lack of connections in this case. This suggests you're either very confused about how objects and delegates work, or you're very new to programming. Either way, trying to use ARToolkit (an Augmented Reality toolkit) is far beyond your demonstrated skill. You need to understand the fundamental relationships and connections between objects before anything else. Go back and review the basics or you'll never get anywhere.
     

Share This Page