uialertview被称为不止一次

这是刺激… !!!

我search了这个问题,发现了一些Relative Questions但不是Satisfactory Answers

所以我有一个- (IBAction)方法,添加一些UITextField's值到NSMutableArray"Add"button被点击。 我只是试图显示UIAlertView ,如果UITextField是空的

我的代码:

 - (IBAction)addButtonPressed:(id)sender { if ([textField1.text length]==0 || [textField2.text length]==0 || !someFlag) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MyApp" message:@"Please Enter Valid Data..." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; [alert release]; } else { // Code to add textField's value to Array. } } 

我的问题 :

每当我用空的textField“添加”button, UIAlertView出现三次

1)首次使用"Close"button显示。 (我从来没有添加…)在一段时间内消失。

2)第二次用“OK”button出现。 (这就是我添加的…)当我按下“确定”button时消失。

3)第三次再次出现“closures”button。 当我按下“closures”button时它消失。

编辑:

类似的问题: UIAlertView每次调用三次而不是一次 。 有人能帮我find解决办法吗?

您的代码不包含任何问题。 没有3只有2个警报。 这里是警报视图的stream程:

  1. 只要你点击addbutton那里调用2select器(可能是一个内部或两个IBAction一个button),其中包含警报视图代码
  2. 现在alert2(带有取消button)在alert1之前被调用(带有OKbutton)
  3. 然后alert1被调用并隐藏alert2
  4. 现在,当你解决alert1(通过点击确定button)alert2再次出现

现在你需要做的是检查“如果你的button没有连接2 IBActions”,这应该是因为你没有这样的代码来调用另一个警报在这种方法。 并检查是否有帮助。

奇怪….!!!

有时会发生,当你过度激怒的时候,你完全忽略了代码的某些行。 它也发生在我身上。 我忽略了一个从-addButtonPressed方法调用的方法,里面有一个AlertView (当然是"Close"button)。

这就是解决scheme本身!

是的,我面对同样的问题,但我的情况是不同于你。

  1. 你应该尝试[textfield.text isEqualToString:@""]; 因为这是比较Objective-C中空白文本字段的标准方法。

  2. 检查您是否正确地解除了您的警报视图有时我们不会关注警报视图,因为您的警报视图保持活动状态,当您重新打开时,您是应用程序,根据您的情况显示2到3次。 因此,您可以使用委托确实closures警报视图与button索引closures在视图中的警报视图确实消失 。 我不知道,但它应该为你工作祝你好运的兄弟。

  3. 而且我不确定,但是我认为每次单击任何button时都会覆盖您的IBActionbutton,因此您还应该检查它。

尝试下面的代码……让我知道它是否工作!

你所做的是你已经给了其他button零2倍..所以可能是这样的问题…

快乐的编码!

  if ([textField.text length]==0) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MyApp" message:@"Please Enter Valid Data..." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; } 

尝试比较像这样。

 if([testBox.text isEqualToString:@""] { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:errorDesc delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } 
 - (IBAction)addButtonPressed:(id)sender { if ([textField.text length]==0) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Information",@"") message:NSLocalizedString(@"Txt is Empty!",@"") delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitles:nil]; [alert show]; alert = nil; } else { // Code to add textField's value to Array. } } 

首先检查点击button时你调用了多less次IBAction方法?

其他明智的把UIAlertView实例是一个公共 ..我的意思是把.h file并将其作为self.yourAlertViewName.m file访问。

谢谢 :)

用下面的代码检查:

 if ([textField.text length]==0) { UIAlertView *objAlertMsg = [[UIAlertView alloc] initWithTitle:@"MyApp" message:@"Please Enter Valid Data..." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [objAlertMsg show]; [objAlertMsg release]; } 

检查一下,我已经设置委托为“无”,而不是“自我”。 如果不需要,请确保您没有在视图控制器中实现委托。

希望这对你有帮助。

干杯。