Я хочу создать настраиваемое оповещение с помощью UIViewController
и добавить его представление в качестве подпредставления к текущему окну. Представление отображается очень хорошо, но я не могу обрабатывать события касания.
Я много раз пытался добавить кнопку в этот контроллер просмотра и добавить цель. Я также пытался добавить UITapGestureRecognizer
и установить представление userInteractionEnabled
на true
, но это также не удалось, даже когда я очистил все подвиды, просто оставив кнопку.
Я что-то пропустил?
Вот код:
Контроллер просмотра 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];
addChildViewController
доaddSubview
. - person tuledev   schedule 30.08.2016