3

在 iOS 上,我以编程方式创建了一个按钮,但事件没有触发。

UIButton * anyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[anyButton addTarget:self action:@selector(handleAnyButton:) forControlEvents:UIControlEventTouchUpInside];
anyButton.frame = CGRectMake(150, 350, 22, 22);
[self addSubview:anyButton];

代码放置在自定义视图(不是自定义视图控制器)中,但我不能让它触发事件。没有显示新闻动画。自定义视图启用了 userInteractionEnabled。我还尝试使用情节提要在同一视图上创建另一个按钮,并且该按钮正在工作。按钮代码在 initWithFrame 中完成。一定是一些我没有发现的简单错误,我已经为此苦苦思索了好几个小时。

编辑:

事件处理程序:

-(void) handleAnyButton:(UIButton *)button
{
    NSLog(@"handleAnybutton called");
}

EDIT2:上述按钮代码在 PKLocationsSelectionView 类的 [self setup] 中完成。

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setup];
    }
    return self;
}

这个视图是在负责这个层次结构的视图控制器(PKLocSelectionViewController)中以编程方式创建的:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    CGFloat fieldsHeight = 88;
    UIView * view = [[PKLocationsSelectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, fieldsHeight)];

    [self.view addSubview:view];
}
4

4 回答 4

3

It looks like your button frame

anyButton.frame = CGRectMake(150, 350, 22, 22);

is outside of parent bounds

CGFloat fieldsHeight = 88;
UIView * view = [[PKLocationsSelectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, fieldsHeight)];
于 2012-12-18T03:50:32.700 回答
0

尝试更改您的 View 初始化代码viewDidLoad,以提供静态框架并查看它是否有效。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    CGFloat fieldsHeight = 88;
    //See the change in below line.. instead of using self.view.bounds.size.width
    // I used a static width.. 320 pixel for the moment..
    UIView * view = [[PKLocationsSelectionView alloc] initWithFrame:CGRectMake(0, 0, 320.0, fieldsHeight)];

    [self.view addSubview:view];
}

viewDidLoad视图中,控制器的层次结构不会完全绘制出来self.view.bounds.size.width。所以可能会给出不正确的值。要访问self.view.frameand self.view.bounds,您至少应该等到viewWillAppear被调用。

此外,您的按钮 origin.y 位于 350 处,在最大高度仅为 88 的视图中。父框架之外的任何控件都不会收到触摸。

于 2012-12-18T03:51:21.183 回答
0

这是对我有用的代码:

MyCustomView.m: 

#import "MyCustomView.h"

@implementation MyCustomView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 100, 100);
        [button addTarget:self action:@selector(greet:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:@"Greet" forState:UIControlStateNormal];

        [self addSubview:button];

        self.backgroundColor = [UIColor greenColor];

    }
    return self;
}

-(void) greet:(id) sender
{
    NSLog(@"greet!");
}

这是 ViewController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];

    MyCustomView *view = [[MyCustomView alloc] init];
    view.frame = CGRectMake(0, 0, 100, 100);
    [self.view addSubview:view];

}
于 2012-12-18T03:37:07.220 回答
0

EDIT2:可能是按钮没有完全封闭在容器(PKLocationsSelectionView)视图的坐标中吗?

PKLocationsSelectionView 的高度是您的代码中的 88,并且按钮的位置似乎在此之外。我认为如果按钮没有包含在超级视图的框架内,那么它将不会收到触摸。

于 2012-12-18T01:25:30.793 回答