问题
It's Irritating ...!!!
I googled about this problem, found some Relative Questions
but not Satisfactory Answers
.
So I have One - (IBAction)
method that adds some UITextField's
Values to NSMutableArray
when "Add"
Button is Clicked. I am simply trying to show UIAlertView
, if the UITextField is empty.
My Code :
- (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.
}
}
My Problem :
Whenever I press "Add" Button with empty textField , UIAlertView
appears thrice.
1) For the First Time It appears with "Close"
Button. (I have never added...) It disappears within some time.
2) Second Time It appears with "OK" Button. (That's what I added...) It disappears when I press "OK" Button.
3) Third Time It appears with "Close" Button again. It disappears when I press "Close" Button.
EDIT :
Similar Question : UIAlertView Pops Up Three Times per Call Instead of Just Once. Can someone help me to found solution from this ?
回答1:
Your code contains no issues. There's not 3 it's only 2 alerts. here's the flow of alert view:
- As soon as you click on
add
button there called 2 selector (may be one inside other OR two IBAction by one button) which contains alert view code in them - Now that alert2 (with cancel button) get called before alert1(with ok button)
- Then alert1 get called and hide alert2
- Now when you resolve alert1(by clicking on ok button) alert2 shows up again
Now what you need to do is to check "if your button is not connected with 2 IBActions", which that should be as you have no such code to call another alert in this method. And check if it helps.
回答2:
Strange....!!!
Sometimes it happens that you Totally Neglect certain lines of your code When you are Over-Irritated. It happend to me also. I neglected one method that is called from -addButtonPressed
Method , Which has One AlertView
(With "Close"
Button of course) inside it.
That's the Solution itself !!!
回答3:
yes I have face same problem but my case is different than you .
You should try
[textfield.text isEqualToString:@""];
because this is the standard way to compare empty text field in Objective-C.Check that you dismiss your alert view properly sometimes we don't give focus on dismissing the alert view so because of that your alert view remain active and when you reopen you are app it shows 2 to 3 times depending on your condition. So you can use the delegate did dismiss alert view with button index for dismissing the alert view in view did disappear. I am not sure but it should work for you Good luck dude.
And I am not sure but I think your IBAction Button is overwrite for each time when you clicked in any button so you should check it also.
回答4:
Try below code......let me know it is working or not!!!!
what you have done is you have given other button nil 2 times..so may be that is the problem...
Happy Coding!!!!
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];
}
回答5:
Try compare like this.
if([testBox.text isEqualToString:@""]
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:errorDesc
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
回答6:
- (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.
}
}
First check that how many times you call IBAction
method when Button tapped???
Other wise put instance of UIAlertView
is a public.. I mean put in .h file
and access it as self.yourAlertViewName
in .m file
.
Thanks :)
回答7:
Check with below code:
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];
}
Check that, I have set delegate to "nil" instead of "self". And make sure you have not implement delegate in view controller if it's not required.
Hope it will be helpful to you.
Cheers.
来源:https://stackoverflow.com/questions/14751187/uialertview-called-more-than-once