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の[自己設定]内で実行されます。

- (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

ボタンフレームのようです

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

親の境界外です

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

でビュー初期化コードを変更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.frameおよびにアクセスするには、呼び出されるself.view.boundsまで少なくとも待機する必要があります。viewWillAppear

また、ボタンのorigin.yは350であり、最大高さが88しかないビューでは、親のフレームの外側にあるコントロールはタッチされません。

于 2012-12-18T03:51:21.183 に答える
0

EDIT2:ボタンがコンテナ(PKLocationsSelectionView)ビューの座標内に完全に囲まれていない可能性がありますか?

PKLocationsSelectionViewの高さはコードから88であり、ボタンの位置はこれの外側にあるようです。ボタンがスーパービューのフレーム内に囲まれていないと、タッチが届かないと思います。

于 2012-12-18T01:25:30.793 に答える
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 に答える