UIView
のサブビューであるUIButton
でタップをキャプチャする際に問題があります。私のコードがどのように設定されているのですか:UIViewのサブビューであるUIButtonをタップします
// in MyClass.m
@interface MyClass()
@property (nonatomic, retain) UIButton *myButton;
@end
@implementation MyClass
@synthesize myButton;
- (void) buttonTapped:(id) sender {
NSLog(@"button tapped!");
}
- (id) initWithFrame:(CGRect)frame {
if (!(self = [super initWithFrame:CGRectZero]))
return nil;
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton setImage:[UIImage imageNamed:@"image.png"]
forState:UIControlStateNormal];
[myButton addTarget:self
action:@selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
myButton.exclusiveTouch = YES;
myButton.frame = CGRectMake(100, 100, 100, 100);
[self addSubview:myButton];
return self;
}
- (void) dealloc {
[myButton release];
[super dealloc];
}
@end
ボタンがビューに表示されます。タップするとボタンの色が瞬時に変わります。ただし、セレクタbuttonTapped:
は呼び出されません。 何か考えてみませんか?
buttonTapped:
が本当にmyButtonのターゲットであることを確認するにはどうすればよいですか?あなたの現在のクラスは、R
NSLog(@"actions for target %@",[myButton actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]);
をログに記録することにより、ターゲットであることを確認できた
フレームがCGRectZeroに設定されているのはなぜですか?また、 '[super release]'ではなく '[super dealloc]'でなければなりません。 – Anna
あなたは正しいです。 '[super dealloc];に更新されました – SundayMonday