views:

43

answers:

1

i wanted to implement an optional protocol for my subviews. the subview-class is inherited by uiviewcontroller and the most viewcontrollers are inherited by this subview. i think it is a simple structure.

the problem is: it only works in the simulator. on a device just the first nslog appears, then the application closes. on the simulator it works fine.

what could it be ?

sure you see, that some things are commented out, but they dont have any effort.

the protocol:

@protocol CustomGestures <NSObject>

@optional

-(void)nextPage;   
-(void)previousPage;

@end

parts of my subviewcontroller

@implementation SubViewController

//handles gestures and passes them
//further to the superclass
//uiviwcontroller to take the normal behaviour

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    //pass the information to superclass    
    [super touchesEnded:touches withEvent:event];   
    //create object with protocol and some other objects    
    id<CustomGestures> gestures;    
    UITouch* touch = [touches anyObject];   
    CGPoint currentPosition = [touch locationInView:self.view];

    CGFloat deltaX = gestureStartPoint.x - currentPosition.x;   
    CGFloat deltaY = fabs(gestureStartPoint.y - currentPosition.y);      
    //if the subclass (self) has the protocol implemented, use the protocoll-features   
    if ([self conformsToProtocol:@protocol(CustomGestures)])
    {       
        if (deltaX <= -80 && deltaY <= 20) { 
            NSLog(@"vorige seite");
            [gestures nextPage]; 
        } else 
            if (deltaX >= 80 && deltaY <= 20) {
                [gestures previousPage];
                 NSLog(@"nächste seite");
             }
         }

     }
}

parts of a subview

 @interface NavInfos :
 SubViewController <CustomGestures>{
 ....}
 @implementation NavInfos
 //CustomGestures-Protocol

 -(void)nextPage{   
    NSLog(@"nächste seite im navinfos");     
    //[[TTNavigator navigator] openURLAction:[TTURLAction actionWithURLPath:@"tt://mapkit"]];//applyAnimated:YES]     
 }
 -(void)previousPage{   
     NSLog(@"vorige seite im navinfos");

    //[[TTNavigator navigator] openURLAction:[TTURLAction actionWithURLPath:@"tt://windows"]];
    //applyTransition:UIViewAnimationTransitionFlipFromLeft] applyAnimated:YES] 
 }
+2  A: 

First of all, since you're declaring those two methods as @optional a check to conformsToProtocol: is not enough to verify that they respond to the methods in question. You should either remove the @optional keyword or add a check to respondsToSelector: as well as the conformsToProtocol: deal.

As for your problem, you are not setting gestures to anything, but you're calling methods in it anyway. You want to set it to something (e.g. self) or switch the two calls from [gestures ...] to [self ....].

Kalle
to the respontsToSelector: i also had this idea before - maybe i am wrong, but if i init a variable with the protocol, this always is true, even if the subclass has not implemented this. maybe, i am doing a fundamental error. in the line UIViewController:SubViewController:anyViewController i want, that SubViewController implements a gesture-aknowledge as above as an optional protocol. so if the anyViewController has the protocol implemented, i want it to be called. if not implemented, it should not be called - or better a standard-code could be called. but for the second i dont have an idea
nico
>You want to set it to something (e.g. self) or switch the two calls from [gestures ...] to [self ....]. this was it, no definition of an object, that was a wrong idea
nico
No, a protocol with optional declarations does not need to declare those, and you must check for their existence, or risk crashing. Believe me, I've crashed over that one. :)
Kalle