【发布时间】:2012-07-18 18:23:15
【问题描述】:
我从我的自定义视图中创建了一个 .xib 文件。 我为该视图创建了 .h/.m 文件。 我 ctrl 从按钮拖动到头文件以创建 IBAction 并将值设置为 touchUpInside。这是正在发生的事情:
http://screencast.com/t/R1WTpK7xp
WTF?
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: 这是我添加两个视图的地方。绘图视图是在代码中创建的,bottomToolbar 是从 .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;
}
【问题讨论】:
-
这可能意味着您连接错误的操作,您有按钮连接检查器的屏幕截图吗?
-
嘿,我用截图编辑了问题。如您所见,它设置在 Touch Up Inside 上。
-
如果在代码中添加动作会发生什么?
-
尝试删除按钮/插座等,清理项目并从头开始(即再次制作按钮/连接)
-
尽管如此,设置一个插座以访问该按钮。然后,在代码中,添加目标/动作(如@Dan 建议的那样)并查看它是否有效......