2013-07-13 102 views
2

我试图在按任意一个按钮(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

你使用的是AlertView的默认按钮,点击该按钮后,它会自动关闭alertview。

所以,你必须以编程方式创建您的按钮,并补充说,在你的alertview按钮,如:在这个BTN

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; 
[btn setTitle:@"+" forState:UIControlStateNormal]; 
[countAlert addSubview: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

+0

我会覆盖哪些方法? @BaZinga – Keeano

+0

你根本不应该覆盖警报。 – Sulthan

+0

@KeeanoMartin看到我编辑的答案 – KDeogharkar

0

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

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

从用户的角度来看,它更加舒适,能够修改滑块中的所有属性或任何其他形式的永久输入,然后,当确定时,用警报视图(或确认屏幕)点击确认按钮, 。在Alert视图中执行此操作不仅容易出错,而且与iOS其余部分的工作方式相反。

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