仍在解雇警报

时间:2013-07-13 07:16:06

标签: objective-c ipad delegates uialertview

按下任一按钮(1& 2)后,我试图保持此UIAlertView

点击“+”按钮或“ - ”按钮后,我可以看到UILabel文字增量,然后关闭UIAlertView

这就是我目前使用的:

#pragma Alert View Methods

-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
{
        [self dismissWithClickedButtonIndex:buttonIndex animated:animated];

}

#pragma count functions
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1 || buttonIndex == 2) {
        return;
    }
    else{

        [self dismissWithClickedButtonIndex:buttonIndex animated:YES];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        self.currentCountButtonCount++;
        [self.countAlert setMessage:[NSString stringWithFormat:@"%d",self.countButtonCount + 1]];

    }if (buttonIndex == 2) {
        self.currentCountButtonCount--;
        [self.countAlert setMessage:[NSString stringWithFormat:@"%d",self.countButtonCount - 1]];

    }
}

- (IBAction)countClick:(id)sender {


    // tallies and keeps current count number

    if (!self.currentCountButtonCount)
         self.currentCountButtonCount = 0;

   NSString *alertMessage = [NSString stringWithFormat:@"%d", self.countButtonCount];

    self.countAlert = [[UIAlertView alloc]initWithTitle:@"Count" message:alertMessage delegate:self cancelButtonTitle:@"end" otherButtonTitles:@"+",@"-", nil];

   [self.countAlert show];
}

关于我的最后一个问题,有人告诉我要自定义,这就是我现在正在尝试的,它仍然会驳回UIAlert。

如何在标签发生变化时保持它直到触摸结束按钮?

2 个答案:

答案 0 :(得分:2)

您使用的是AlertView的默认按钮,点击该按钮后,它将自动关闭警报视图。

因此,您必须以编程方式创建按钮,并在警报视图中添加这些按钮,如:

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[btn setTitle:@"+" forState:UIControlStateNormal];
[countAlert addSubview:btn ]; 

在这个btn上调用你的方法。

所以你必须用“+”和“ - ”创建两个自定义按钮。并在AlertView中添加该按钮。

-(void)setAlertValue:(id)sender{

    switch ([sender tag]) {
        case 1:
        {
           // currentCountButtonCount++;

            [self.countAlert setMessage:[NSString stringWithFormat:@"%d",++countButtonCount]];
        }
            break;
        case 2:
        {
            //currentCountButtonCount--;

            [self.countAlert setMessage:[NSString stringWithFormat:@"%d",--countButtonCount]];
        }
            break;
        default:
            break;
    }
}

- (IBAction)countClick:(id)sender {
// tallies and keeps current count number

    if (!currentCountButtonCount)
        currentCountButtonCount = 0;

    NSString *alertMessage = [NSString stringWithFormat:@"%d", countButtonCount];

    self.countAlert = [[UIAlertView alloc]initWithTitle:@"Count" message:alertMessage delegate:self cancelButtonTitle:@"end" otherButtonTitles:nil, nil];


    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 50, 40, 20)];
    [btn addTarget:self action:@selector(setAlertValue:) forControlEvents:UIControlEventTouchUpInside];
    [btn setBackgroundColor:[UIColor greenColor]];
    btn.tag = 1;

    [btn setTitle:@"+" forState:UIControlStateNormal];

    UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(230, 50, 40, 20)];
    [btn1 addTarget:self action:@selector(setAlertValue:) forControlEvents:UIControlEventTouchUpInside];
    [btn1 setBackgroundColor:[UIColor redColor]];
    btn1.tag = 2;

    [btn1 setTitle:@"-" forState:UIControlStateNormal];
    [countAlert addSubview:btn];
    [countAlert addSubview:btn1];
    [self.countAlert show];

}

enter image description here

答案 1 :(得分:0)

我不想在你的游行中下雨,但如果你认为警报视图是处理变量增量/减量的最佳方法,我建议你重新考虑你的设计。

UIAlertViews用于瞬态信息和简化决策。一个简单的“你确定吗?”是警报视图用法的教科书示例。

从用户角度来看,能够修改滑块中的所有属性或任何其他形式的永久输入更令人感到欣慰,然后,当确定时,点击确认按钮并显示警报视图(或确认屏幕)。在警报视图中执行此操作不仅容易出错,而且与其他iOS工作方式相比具有反直觉性。

如果您在应用程序中遇到其他形式的输入时遇到问题,请阅读如何执行动画并在需要时显示控件,将输入隐藏在UIAlertView中只是最简单的解决方案,但不是对用户来说是最好的。