uialertview called more than once [closed] - iphone

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
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 ?

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.

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 !!!

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.

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];
}

Try compare like this.
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.
}
}
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 :)

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.

Related

How the UIActionSheet works?

When popping a view I want to save some data, by asking confirmation. I'm asking confirmation using UIActionSheet. But irrespective of my response in action sheet, the view is changing in background, it creates some problem for me to use the response. I'm using navigation controller to switch views. How can I solve this
TIA
Better option is to use uialertview for asking confirmation.To do this follow this step:
Insure your header file contains the following:
#interface YourViewController : UIViewController <UIAlertViewDelegate>
Now when asked confirmation add this code:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message" message:#"Are You Sure" delegate:self cancelButtonTitle:#"YES" otherButtonTitles:#"NO", nil];
[alert show];
[alert release];
Now after pressing one button below delegate will be called so add in .m file of app
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the YES/NO buttons
if (buttonIndex == 0)
{
NSLog(#"NO button pressed");
}
else
{
//Save data here
NSLog(#"YES button pressed");
}
}
#PooLas If I understood you correctly, You use uiactionsheet for user confirmation, while in background (actually under actionsheet) you change view controllers. Well, you can't do that, because delegate must be attached to controller which shows it up (if i am wrong, correct me). So when you click button, you can only first dismiss actionsheet and then change view controller, but not opposite – PooLaS

How to show an AlertView with another AlertView

I want to show nested alertViews. Problem, which i am facing in nested alertViews is that when i click an "add" button of first alertView it shows the second alertView, in second alertView i have a textField and a "Save" button. I want to save data when i click on save button and then reload UITableViewData, which is already in the first alertView.
I am new in iphone, so please help me.
You should create your alert views with different tag property so that in delegate method you can easily differentiate which alert view is appeared on the screen.
For example :
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Info"
message:#"Message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil] autorelease];
[alert setTag: 1001]; // give different tag to different alert views
[alert show];
[alert release];
Now in delegate method :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1001)
{
// do something
}
eles if (alertView.tag == 1002)
{
}
}
Hope it helps you..

How to prevent TTPostController's modal view from being dismissed upon errors?

For those of you who use three20's TTPostController, how do you go about handling user errors before and after sending a request? i.e., if a user tries to post a blank comment, I want to be able to alert them and keep the view active so they can fix it.
The problem I'm having though is getting the modal view to stay open after things go wrong. I can alert the user, but once they click OK, the modal gets dismissed. I thought I was going be able to use TTPostControllerDelegate:willPostText delegate to accomplish this, but that doesn't seem to be working, or I'm not understanding exactly how it's designed to work.
What i'm doing:
/**
* The user has posted text and an animation is about to show the text return to its origin.
*
* #return whether to dismiss the controller or wait for the user to call dismiss.
*/
- (BOOL)postController:(TTPostController*)postController willPostText:(NSString*)text {
if ([text length] == 0) {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Your message is blank"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil ];
[alert show];
[alert release];
return false;
}
return true;
}
If I try posting a blank comment, I get an alert letting me know, but once I press OK, the modal view get's dismissed.
Is there something I'm missing or not understanding correctly?
Edit: I should also note that I tried using alertView's didDismissWithButtonIndex: method to try and stop the view from unloading, but i wasn't successful.
I ran into this the other day. If you pass nil instead of self as the delegate into:
UIAlertView initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles
the TTPostController is no longer dismissed when the user touches "OK".
- (BOOL)postController:(TTPostController*)postController willPostText:(NSString*)text {
if ([text length] == 0) {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Your message is blank"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil ];
[alert show];
[alert release];
return false;
}
return true;
}

IPHONE: UIAlertView called twice in a custom function/IBAction

i have an IBAction which does some processing and within will have a few UIAlertViews (to show alerts). However it seems that the FIRST alert in the paragraph is being called TWICE (once immediately after i clicked and another time after all the other alerts has occured). Additionally, the first time the alert appears, the alert automatically closes even though i have an OK button and the user has not clicked on it. The 2nd time the alert appears, it will require the user to click on OK.
I tried moving the paragraph out from IBAction into its own function but still the problem occurs.
all the alerts in my IBAction/function are the same:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"blah" message:#"blah" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil,nil];
[alert show];
[alert release];
but the other alerts function normally.
the code looks like this ("blah" is the one being called twice):
-(void)function {
if (......) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"blah" message:#"blah" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil,nil];
[alert show];
[alert release];
for (int i=0; i<2; i++) {
if (.....) {
//do stuff
} else {
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"blah2" message:#"blah2" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil,nil];
[alert2 show];
[alert2 release];
}
}
} else {
UIAlertView *alert3 = [[UIAlertView alloc] initWithTitle:#"blah3" message:#"blah3" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil,nil];
[alert3 show];
[alert3 release];
}
}
please help!
First of all, we need more code to diagnose your problem. What you provide is not sufficient.
Second, I once encountered a similar problem: when a compose an email action is triggered by the user who didn't set up her email account on that device, I asked my app to show an UIAlertView. However, when I tested my code on a real device with such a scenario, two consecutive UIAlertViews showed, one after another, both of which are about the email account not set up issue.
I finally figured out that the iOS system will automatically show an UIAlertView when the email account is not set up while a user tries to compose an email, which is why two UIAlertViews showed up when I only expected one.
Hope that helps.

How to add UITableView to UIAlertView?

I create a class:
#interface myUITableViewController : UIViewController
{
NSArray *listData;
}
...
and later,I do so:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
myUITableViewController *myUITable = [[myUITableViewController alloc] init];
[alert addSubview:myUITable.view];
[alert show];
After running,the result is that the myUITable.view's size is bigger than alert.
Why?
Please tell me if you know.
Thank you!
UIAlert is not really meant to be used like that. You should make your own custom UIView and add whatever content you need in it (the table and buttons). Then handle how it shows and hide yourself.
Even if you manage to get it shown correctly chances are it might break in the future. In one of my apps i was showing an alert with a UITextField. I was making space for it by adding "\n" to the message. In later iOS versions this stopped working and it looked really awful...