0

カスタム アラートを作成しUIViewController、そのビューをサブビューとして現在のウィンドウに追加したいと考えています。ビューは非常によく表示されますが、タッチ イベントを処理できません。

このビューコントローラーにボタンを追加してターゲットを追加しようと何度も試みました。また、ビューを に追加してビューを設定しようとしましたがUITapGestureRecognizer、すべてのサブビューをクリアしてもボタンを離れても失敗しました。userInteractionEnabledtrue

私は何かを逃しましたか?

コードは次のとおりです。

CustomAlert ビューコントローラー:

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

    self.view.backgroundColor = [UIColor clearColor];

    backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    backgroundView.backgroundColor = [UIColor blackColor];
    backgroundView.alpha = 0.5;
    [self.view addSubview:backgroundView];


    backImage = [[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 300) / 2, (self.view.frame.size.height - 250) / 2, 300, 250)];

    backImage.image = [UIImage imageNamed:@"AlertBackground"];
    [self.view addSubview:backImage];

    confirmButton = [[UIButton alloc]initWithFrame:CGRectMake( backImage.frame.origin.x + 100 , backImage.frame.origin.y + 250 - 40, 100, 26)];
    [self.view addSubview:confirmButton];
    [confirmButton addTarget:self action:@selector(confirmButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    confirmButton.backgroundColor = [UIColor redColor];
    [confirmButton setTitle:@"click me" forState:UIControlStateNormal];
}

-(void)confirmButtonClick:(UIButton*)sender{

    [self.view removeFromSuperview];
}

-(void)show{
    UIWindow * window = (UIWindow*)[UIApplication sharedApplication].keyWindow;
    [window addSubview:self.view];
}

メソッド呼び出しのカスタム アラート:

CustomAlert * alert = [[CustomAlert alloc]init];
[alert show];
4

1 に答える 1