如何使用自定义视图模仿UIAlertView模式行为?

时间:2012-10-10 01:40:06

标签: ios6 modal-dialog uitouch

我有一个视图,如果在您尝试保存时未填充项目条目的名称字段,则弹出UIAlertView。我想改为显示自己的自定义警报视图。我用xib加载它:

- (void)enterNameAlert {

    NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"AddNameAlertView" owner:self options:nil];
    enterNameAlertView = [subviewArray objectAtIndex:0];
    enterNameAlertView.frame = CGRectMake(232, 417, 303, 171);
    enterNameAlertView.alpha = 0.0;
    [self.view addSubview:enterNameAlertView];
    [self.view bringSubviewToFront:enterNameAlertView];

    //fade view in
    [UIView animateWithDuration:.50f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(void) {

        enterNameAlertView.alpha = 1.0;

    } completion:NULL];

    [UIView commitAnimations];

}

但是,在显示警报时,您仍然可以单击视图上的所有其他元素。理想情况下,我想制作alertview模式。作为一个hack,我只是在alertview启动时将xxxx.enabled = NO设置为所有其他元素,并在警报上按OK时将它们切换回xxxx.enabled = YES。

必须有更好的方法取消对self.view的所有触摸,但不取消self.enterNameAlertView。

如果我执行self.view.userInteractionEnabled = NO,则取消对self.view以及self.enterNameAlertView的所有触摸。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

我认为您的问题的解决方案是您必须在自定义警报视图和原始视图之间引入一个视图,一旦警报到来,您需要将该视图带到您的self.view并隐藏(或删除)当你的警报被解雇时,我已经这样做了,它对我来说就像魅力一样。希望这也有助于你:)

答案 1 :(得分:0)

知道了。我使用了启用了交互的UIScrollView。这会阻止自定义警报视图外部的触摸传递到下面的主视图。

- (void)enterNameAlert {

    //pop a UIScrollView behind the alert so that the user can't touch the controls behind it
    shadowScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
    shadowScrollView.userInteractionEnabled = YES;
    shadowScrollView.backgroundColor = [UIColor blackColor];
    shadowScrollView.alpha = 0.0;
    [self.view addSubview:shadowScrollView];
    [self.view bringSubviewToFront:shadowScrollView];

    //put the alertview in front of the disabled view to make it act like a modal view
    NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"AddNameAlertView" owner:self options:nil];
    enterNameAlertView = [subviewArray objectAtIndex:0];
    enterNameAlertView.frame = CGRectMake(232, 417, 303, 171);
    enterNameAlertView.alpha = 0.0;
    [self.view addSubview:enterNameAlertView];
    [self.view bringSubviewToFront:enterNameAlertView];

    //animate view in
    [UIView animateWithDuration:.50f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(void) {

        shadowScrollView.alpha = .6;
        enterNameAlertView.alpha = 1.0;

    } completion:NULL];

    [UIView commitAnimations];

}