我从我的自定义视图中创建了一个 .xib 文件。我为该视图创建了 .h/.m 文件。我 ctrl 从按钮拖动到头文件以创建 IBAction 并将值设置为 touchUpInside。这是正在发生的事情:
http://screencast.com/t/R1WTpK7xp
怎么回事?
当 up 在按钮之外时触发事件?
编辑:
这是屏幕截图:
反对票是怎么回事?我看不出这有什么意义。
查看.h
#import <UIKit/UIKit.h>
#import "DrawingViewDelegate.h"
@interface DrawingBottomToolbarView : UIView
@property (weak) id <DrawingViewDelegate> delegate;
- (IBAction)lineSegmentButtonPush:(id)sender;
@end
查看.m
#import "DrawingBottomToolbarView.h"
@implementation DrawingBottomToolbarView
@synthesize delegate;
- (id)initWithFrame:(CGRect)frame
{
NSLog(@"frame");
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"DrawingBottomToolbarView" owner:self options:nil] objectAtIndex:0]];
//[[[NSBundle mainBundle] loadNibNamed:@"DrawingBottomToolbarView" owner:self options:nil] objectAtIndex:0];
//[self addSubview:self.];
}
return self;
}
//-(id)initWithCoder:(NSCoder *)aDecoder{
//
// NSLog(@"coder");
// if ((self = [super initWithCoder:aDecoder])){
// [self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"DrawingBottomToolbarView" owner:self options:nil] objectAtIndex:0]];
// }
// return self;
//}
- (IBAction)lineSegmentButtonPush:(id)sender
{
NSLog(@"line push");
}
@end
我不明白问题出在哪里。
编辑2:
我尝试将按钮设置为插座并在代码中添加目标/动作,同样的事情发生了:
。H
@property (weak, nonatomic) IBOutlet UIButton *lineSegmentButton;
.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"DrawingBottomToolbarView" owner:self options:nil] objectAtIndex:0]];
self.currentSelectedPathSegment = NoneSegment;
[self.lineSegmentButton addTarget:self action:@selector(lineSegmentButtonPush:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
编辑 3:这是我添加两个视图的地方。绘图视图是在代码中创建的,底部工具栏是从 .xib 文件创建的。
kBottomToolbarHeight 是常数,其值与 .xib 文件中定义的高度相同。
- (void)viewWillAppear:(BOOL)animated
{
[self.view addSubview:self.drawingView];
[self.view addSubview:self.bottomToolbar];
CGRect selfRect = self.view.bounds;
CGRect drawingViewRect = selfRect;
CGRect bottomToobarRect = selfRect;
drawingViewRect.size.height = selfRect.size.height - kBottomToolbarHeight;
bottomToobarRect.size.height = kBottomToolbarHeight;
bottomToobarRect.origin.y = drawingViewRect.size.height;
self.drawingView.frame = drawingViewRect;
self.bottomToolbar.frame = bottomToobarRect;
}