UIAlertView shows up several times after Home Button is pressed - iphone

Iam calling this Code in my ViewController if some Settings are not set or are (by the length) zero:
UIAlertView *infoView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Achtung", nil) message:NSLocalizedString(#"Wrong Settings",nil)delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[infoView show];
[infoView release];
The Alertview shows up and if I press the Home Button and start the App again it shows the Alert zwice, after that if i press the Home Button again and start the App the Alert shows up 4 times.....
Why the hell is it like this?
I tried to define a new function for it and tried many other workarounds but nothing worked out for me.
Somebody any Suggestions?

Something like this
- (void)viewWillDisappear {
[super viewWillDisappear];
[myAlert dismissWithClickedButtonIndex:0 animated:NO];
}

Related

adding ok button at run time in UIAlertview ios

I call alert with progress indicator view while calling web services.i am having an alert view set up like this:
[self.activityIndicatorView setHidden:NO];
self.alertView = [[UIAlertView alloc] initWithTitle:#"Sending Login Request.."
message:#"\n"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.activityIndicatorView.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur
[self.alertView addSubview:self.activityIndicatorView];
[self.activityIndicatorView startAnimating];
[self.alertView show];
Later if login fails I want to put "OK" button on alert view, withot dismissing self.alertView, and again showing new instance of self.alertView.Some thing like this:
if (isThereErrorFromJsonResp) {
[self.activityIndicatorView stopAnimating];
[self.activityIndicatorView removeFromSuperview];
self.activityIndicatorView = nil;
[self.alertView setTitle:isThereErrorFromJsonResp];
//here i want to show ok button how?
return;
}
So how should i put OK button?
Any Suggestion?
Remove the alert on getting the response and display an new instance of alert like this
[self.alertView dismissWithClickedButtonIndex:0 animated:YES];
self.alertView = [[UIAlertView alloc] initWithTitle:isThereErrorFromJsonResp
message:#"\n"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[self.alertview show];
SOLUTION
Ok tried out and got it
Use
[alertView dismissWithClickedButtonIndex:0 animated:YES];
[alertView addButtonWithTitle:#"Ok"];
[alertView show];
This will add the button to the alertview
Look at using ATMHud instead - this is a heads-up-display that you can modify while its showing, and can show, start, stop, spinners, add messages etc. When I used it I had a message say "Tap to Cancel", then when the login succeeded, showed a "Success!" for a second or so, then made it disappear. This looks HUD looks very professional in the way it animates, and you have a lot of control over it too.
Try this code:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"Logout in offline mode may cause of data lose. Do you still want to logout?" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"YES",nil];
alert.tag=11;
[alert show];
[self performSelector:#selector(go:) withObject:alert afterDelay:1.0];
-(void)go:(UIAlertView*)alert
{
UIButton *b = (UIButton*)[alert viewWithTag:1];
b.titleLabel.text = #"test";
}
you will have to add "OK" button initially. And set its property as Hidden = TRUE. and in go method set it property Hidden = FALSE

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

Can't see my alertView unless I comment out automatic dismission

I am implementing a simple alert view with activity indicator to show to the user while the app is performing the restore of the database. Everything is fine except that I can't see the alert view, unless I comment out the automatic dismiss line (last one here).
Ok, the restore of the database is quite quick, but still I would expect to see it even if for an instant, no? I can see the screen get's a little darker during animation but that's all. I also tried putting a for loop to extend the time, the time extends but still no alert view.
There is nothing wrong in the way the alert view is called, since if I comment the dismiss, I can see it...only forever. Anyone has an idea here?
Before anyone says it, I tried changing the delegate of the alertView to self as posted here, but it didn't help.
// First we prepare the activity indicator view to show progress indicator
UIActivityIndicatorView * activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activityView setFrame:CGRectMake(121.0f, 50.0f, 37.0f, 37.0f)];
[activityView startAnimating];
// Then we put it in an alert view
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Loading" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[alertView addSubview:activityView];
[alertView show];
// Finally we operate the restore
[[SQLManager sharedSQL] restoreDatabase:[restoreDatabaseURL path]];
// And we can dismiss the alert view with the progress indicator
[alertView dismissWithClickedButtonIndex:0 animated:NO];
Im not sure why this happens, but I encounter it occasionally as well. One way I have gotten something like this to work is by using performSelector:withObject:afterDelay: on the next call, like this:
// First we prepare the activity indicator view to show progress indicator
UIActivityIndicatorView * activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activityView setFrame:CGRectMake(121.0f, 50.0f, 37.0f, 37.0f)];
[activityView startAnimating];
// Then we put it in an alert view
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Loading" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[alertView addSubview:activityView];
[alertView show];
// Finally we operate the restore
// [[SQLManager sharedSQL] restoreDatabase:[restoreDatabaseURL path]];
[[SQLManager sharedSQL] performSelector:#selector(restoreDatabase:) withObject:[restoreDatabaseURL path] afterDelay:0.25];
// And we can dismiss the alert view with the progress indicator
[alertView dismissWithClickedButtonIndex:0 animated:NO];
performSelector Documentation
Sounds like it's dismissing itself almost instantaneously. If you add a for loop, it will likely freeze all other computations which means your activity view won't show up until the loop breaks.
You could try using a NSTimer to call another function to determine if the restore is complete or not;
// Setup our the timer
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(checkRestore)
userInfo:nil
repeats:YES];
// Timer function checking for completion
-(void)checkRestore {
if (restore is complete) {
[alertView dismissWithClickedButtonIndex:0 animated:NO];
}
}

Why isn't UIAlertView Showing?

For some reason screen gets dark and freezes, alert is not shown... can someone please help?
Thanks in advance!
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hello!"
message:#"Hello!" delegate:self
cancelButtonTitle:#"Done"
otherButtonTitles:nil];
[alert show];
[alert release];
}
You are probably calling show from a background thread, call it on the main thread like this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hello!"
message:#"Hello!" delegate:self
cancelButtonTitle:#"Done"
otherButtonTitles:nil];
[alert performSelectorOnMainThread:#selector(show)
withObject:nil
waitUntilDone:NO];
[alert release];
Delegate is correct, but maybe because your do a release at the end it may cause a problem.
Try with a nil delegate :-)
For example :
UIAlertView *alertView;
alertView = [ [ UIAlertView alloc ] init ];
[ alertView setMessage:#"Hello World" ];
[ alertView show ];
[ alertView release ];
If it works, then it was the delegate and you need to declare the variable as a class var. Or it maybe be elsewhere.
Is this alert maybe sitting in a big loop and you are not running on multiple threads? The screen darkening and nothing happening is something I equate with running a long process on the main thread (so the UI doesn't refresh and show the alert).
You get a dark screen without a popup, or slower popup if you show the UIAlertView from a background thread. Just out it back in the main thread and it will be fine. I just had this problem last week.

iPhone, I want to create a popup menu from my tab bar, but what are they call, like copy / paste?

I'd like to create a popup menu, from my tab bar.
I've seen them, but I'm not sure what they called ?
Kind of like a speak bubble.
I think copy / paste is the sort of thing I mean.
You are searching for the UIMenuController.
This question might help you.
That could be the UIActionSheet. You might want to give it a try.
//Your delegate for pressed buttons.
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//Your Action for pressed button.
[actionSheet release];
}
//Declare a method to show your sheet
-(void)showActionSheet
{
UIActionSheet *menu = [[UIActionSheet alloc]
initWithTitle: #"Action Sheet Title"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Delete"
otherButtonTitles:#"Other Button1", #"Other Button2", nil];
[menu showInView:self.view];
}
But you said "bubble", so that could also be the UIAlertView.
Try this:
-(void)showAlertView
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert!"
message:#"This is the message that pops up in the bubble."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
On the iPad, you'd want UIPopoverController, but I think that on the iPhone the same sort of interactions are meant to be performed by a separate, full screen controller or by a UIActionSheet. There's no speech bubble on the iPhone such that I'm aware.