1

I have two .m files. The first is the main code, The second is a subclass of UIImageView so that i can detect touches.
In the main .m file I have added a progress bar and a customimageview both subviews of a scrollview.

What I need is that when a user touches the customimageview that the progress bar moves up and a double tap decreases the [Note: the customimageview has to have its touches recognised in the second .m because of them being in a subview of a scrollview and other controls are having to be handled]

In the main .m file I have a two methods:

- (void)pumpsingletap {  
   progbm.progress +=0.1;  
}  

- (void)pumpdoubletap {  
   progbm.progress -=0.1;  
}  

then in the subclassed uiimageview i have:

//inside touches method
if ([touch view].tag == 555) {  
   NSLog(@"pump touched");  
   switch ([allTouches count]) {  
         case 1: {  
                switch ([touch tapCount]) {  
                     //---single tap---  
                     case 1: {  
                     NSLog(@"single pump touch");  
                     [self performSelector:@selector(pumpsingletap) withObject:nil afterDelay:.4];  
                     } break;  
                     //---double tap---  
                     case 2: {  
                     NSLog(@"double pump touch");  
                     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(pumpsingletap) object:nil];  
                     [self performSelector:@selector(pumpdoubletap) withObject:nil afterDelay:.4];  
                     } break;  
               }  
           }  
       }  
  }

So the NSlog's appear so the touch recognition isn't an issue. But the performSelector falls over. As the customimageview pumpsingletap doesnt work.

So how do i call the method in the subclass.

//update//

so I have added in the following code, in my subclass

mainMethod* callingMethod = [[mainMethod alloc] init];  
[callingMethod performSelector:@selector(pumpsingletap) withObject:nil afterDelay:.4];  

then in my main method for pumpsingletap i changed it to:

- (void)pumpsingletap { 
   NSLog(@"single pump method called");
   progbm.progress +=0.1;  
} 

The NSLog for single pump method called appeared but the progress bar progbm - didn't move. so i have solved my calling issue - just need to now work out why the progress bar isnt moving!!

sregorcinimod
  • 277
  • 3
  • 12

3 Answers3

0

If I'm following the code correctly, you are performing the selector on self, but self is your derived UIImageView class (so you probably crash at that point?). You need to perform selector on the class in your main file. Pass a reference to that to your derived class. Alternately, you could create a delegate, implement it in your main class, pass your main class to the UIImageView and then call through the delegate (there are even other ways to do it (key-value observation), but one of these should work).

David Neiss
  • 8,161
  • 2
  • 20
  • 21
0

I'm not sure if this is the problem, but you don't need brackets around case statements. Also I don't get why you would make a switch within a switch. Just make an if-elseif-else statement, it'll probably be easier to understand.

Other than this, from what I understand, you have a view controller with both a progress bar and a customimageview as properties, and you have methods that should be called in response to certain actions, (tapping or double tapping the customimageview) but they're in the view controller. The usual way to solve this is by using the target action mechanism. UIControls implement the target action mechanism by encapsulating target-action pairs and storing them in a dictionary, keyed by the event type (UIControlEvent). Here's a slightly simpler version.

In the .h file for your subclass of UIImageView, before the @interface write this:

typedef enum {
     ControlEventTap = 0,
     ControlEventDoubleTap 
} ControlEvent;

Then in the .m file add this before the @implementation:

@interface TargetActionPair : NSObject {
     id target;    
     SEL action;
} 

@property (nonatomic, assign) id target;
@property (nonatomic, assign) SEL action;

@end

@implementation TargetActionPair

@synthesize target, action;

@end

Then, add an NSMutableArray instance variable, (but not a property) and a - (void)setTarget:(id)t action:(SEL)a forEvent:(ControlEvent)e method to your customimageview implementation.

The method should look like this:

- (void)setTarget:(id)t action:(SEL)a forEvent:(ControlEvent)e {
    TargetActionPair *tar_act = [[TargetActionPair alloc] init];
    tar_act.target = t;
    tar_act.action = a;
    // actionsArray is the mutable array instance variable and must be allocated and set in the init method for customimageview.
    [actionsArray replaceObjectAtIndex:(NSUInteger)e withObject:tar_act];
    [tar_act release];
}

Then you can replace your touch handling code with:

if ([touch view].tag == 555) {
    NSUInteger tapcount = [touch tapCount];
    if (([alltouches count] == 1) && (tapcount <= [actionsArray count])) {
        TargetActionPair *tar_act = [actionsArray objectAtIndex:tapcount-1];
        [tar_act.target performSelector:tar_act.action withObject:nil afterDelay:.4];
        if (tapcount == 2) {
            TargetActionPair *tar_act2 = [actionsArray objectAtIndex:tapcount-2];
            [NSObject cancelPreviousPerformRequestsWithTarget:tar_act2.target selector:tar_act2.action object:nil];
        }
    }
}

With this code you simply set the target and action for each control event in the viewDidLoad method of the view controller that contains the customimageview. So the calls would look like this:

[self.customimageview setTarget:self action:@selector(pumpsingletap) forEvent:ControlEventTap];
[self.customimageview setTarget:self action:@selector(pumpdoubletap) forEvent:ControlEventDoubleTap];

DON'T FORGET to release the actionsArray in your dealloc method and to be very careful about releasing the view controller since the customimageview doesn't retain it.

I hope this helps, best of luck on your app.

Rich
  • 1,082
  • 7
  • 11
0

In the end I solved the issue by using NSNotificationCenter

sregorcinimod
  • 277
  • 3
  • 12