Tuesday, December 3, 2013

Strange UIButton click event

Define a custom button

@ Interface ImageButton: UIButton {
NSString * _gotoImage;
}

@ Property (nonatomic, retain) ImageButtonData * data;
- (Id) initWithButtonData: (ImageButtonData *) buttonData;
- (Void) buttonClicked: (id) sender;
@ End

Implementation file
- (Id) initWithButtonData: (ImageButtonData *) buttonData {

data = buttonData;

_gotoImage = [NSString stringWithFormat: @ "123% @", data.gotoImage];

/ / Set button frame
[Super initWithFrame: data.rect];

[Self addTarget: self action: @ selector (buttonClicked :) forControlEvents: UIControlEventTouchUpInside];
return self;
}

- (Void) buttonClicked: (id) sender {
NSLog (@ "sendInfo:% @", _gotoImage);
}
@ End


Error
appear in;: ("% @ sendInfo", _gotoImage @) When you click Button, the program runs to NSLog Thread1: program recieved signal "EXE_BAD_ACCESS"

Understand, ask for advice!
Reply:
_gotoImage has been release
Reply:
_gotoImage = [NSString stringWithFormat: @ "123% @", data.gotoImage];
Changed
_gotoImage = [[NSString alloc] initWithFormat: @ "123% @", data.gotoImage]; try. You may buttonData been manually release it.
Reply:
Should have been release ... You can see that the breakpoint to which the
Reply:
You have to retain,
_gotoImage = [[NSString stringWithFormat: @ "123% @", data.gotoImage] retain]; \

Reply:
If you use the ARC, on it. If there is no arc, is the first to retain.

No comments:

Post a Comment