Question

I'm pretty new to objective c and iOS development, and I feel sometimes like I'm banging my head into a brick wall...

I'm adding an image programmatically to the screen to server as a menu button. I have a class handling that button. This is the .h file:

#import <Foundation/Foundation.h>

@interface MenuButton : NSObject <UIGestureRecognizerDelegate>

@property UIImageView * menuButton;
- (id) initWithButton: (UIImageView *) mb;
- (void) onTapMenuButton;
@end

And this is the .m file:

#import "MenuButton.h"

@implementation MenuButton

- (id) initWithButton: (UIImageView *) mb {
    self = [super init];

    if(self) {
        self.menuButton = mb;

        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapMenuButton)];
        tap.numberOfTapsRequired = 1;
        [self.menuButton addGestureRecognizer:tap];
        tap.delegate = self;
    }

    return self;
}

- (void) onTapMenuButton {
    NSLog(@"SUCCESS!");
}

@end

Whenever I tap the button, the program stops running. In the output window all I can see is some text: "(lldb)", and the editor shows me the main function, highlighting the only line I have there, which I haven't touched at all (the one created automatically when I start a single-view application).

What am I doing wrong?

Was it helpful?

Solution

I suspect that you're creating a MenuButton instance, which sets up this tap gesture recognizer, but you're not retaining the the MenuButton instance and it becomes deallocated. The result: the tap gesture recognize is calling onTapMenuButton on an object that no longer exists.

How are you keeping hold of the MenuButton objects?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top