I have some destructive operations that need confirmation, and I'm using the UIActionSheet.
Inside the clickedButtonAtIndex I fire some lenghtly operations that need a UIActivityIndicatorView.
The problem is that I can't see the UIActivityIndicatorView until the UIActionSheet has closed, and this happens when the operation has finished.
Can I force to close the UIActionSheet even if the user has pressed some buttons and their clickedButtonAtIndex delegate has been fired ?
In pseudo code:
User wants to do some destructive operation
Request confirmation with a UIActionSheet
The user says YES
Delegate clickedButtonAtIndex fires and invokes a lenghtly operation
This operation, the first thing that does is opening a UIActivityIndicatorView but because the UIActionSheet is active, I can't see until the end ...
thanks,
r.
You can call
CFRunLoopRunInMode(UITrackingRunLoopMode, 0.1, true);
to let the UI elements refresh before your lengthy calculation begins. Alternatively, you can use
-(void)doLengthyOperation {
show_activity_indicator_view();
[self performSelector:#selector(doRealLengthyOperation) withObject:nil afterDelay:0.1];
}
-(void)doRealLengthyOperation {
// do the real lengthy operations.
}
Related
in my program I have two ways to dismiss an uialertview: one by button click and one by nstimer. If the button is clicked before the timer fires my program quits when the timer fires. How do I check the status of the uialertview so that the timer knows whether or not to execute a dismiss message? And what NSLog statement should I use for debugging.
My guess is that you're app is crashing with an exception because you got stale pointer to the deallocated UIAlertView. I guess that you're not resetting the instance variable that references the UIAlertView.
You surely got an instance variable to save the UIAlertView pointer. You also need to set the delegate property, like this:
myAlert = [[UIAlertView alloc] initWithTitle:...];
myAlert.delegate = self;
[myAlert show];
// Now setup the timer
Your class must implement the UIAlertViewDelegate protocol, specifically you need to implement alertView:willDismissWithButtonIndex: and/or alertView:didDismissWithButtonIndex: (I'd use the later). In this method, you reset the instance variable:
[myAlert release];
myAlert = nil;
// Also cancel timer
Canceling the timer isn't that important any more as setting the variable to nil is the important part. Once the timer would fire it wouldn't crash any more (but canceling the timer would still be a good idea).
I have a UIAlertView with the following delegate method:
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
But I have a problem. In that method, I do some logic and perform some lines of code which take a small amount of time. I would like to dismiss the alertView before I do all of this. I want to dismiss the alert view at the very top of that method. That way, when the user taps a button on the alertView, the app doesn't seem frozen for a second while the next lines of code are executed.
To dismiss a alert programaticly do:
/* Your Processing Code Here */
[theAlertView dismissWithClickedButtonIndex:0 animated:YES];
The [theAlertView dismissWithClickedButtonIndex:0 animated:YES]; will make the alert go away
Either use the delegate method -alertView:didDismissWithButtonIndex: instead—it gets called once the alert view’s been removed from the screen, which will at least conceal the lag your app’s having—or, better, use a background thread, e.g. with -performSelectorInBackground:withObject:, to handle whatever processing you need to do.
Use the other delegate method for did, instead of will:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
You should probably also execute the other lines of code you mention asynchronously, so as not to freeze the UI at all.
I'm looking for a way, to automatically dismiss an alert view after some time or after a task is done.
Is there a possibility? (or another way to show a message for some time?)
You can call the -dismissWithClickedButtonIndex:animated: method to dismiss the alert view.
To dismiss it automatically, create an NSInvocation and then use -performSelector:withObject:afterDelay: to -invoke it.
UIAlertView has a method called:
- ( void )dismissWithClickedButtonIndex: ( NSInteger )buttonIndex animated:( BOOL )animated
You can call it on your UIAlertView object to simulate a button press.
To dismiss it automatically after some time, you will need something like an NSTimer, to check if the alert view is still displayed, and in such a case, dismiss it.
I have a UIAlert that pops up 3 times every time it is called. It appears and then disappears before I can click on it. Could it be that the viewDidLoad itself is being called 3 times?
I implemented an UIAlert in the viewDidLoad of my app:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:alertMessage delegate:self cancelButtonTitle:ok otherButtonTitles:nil];
This is the viewDidLoad in the rootViewController, that manages the views:
- (void)viewDidLoad {
Kundenkarte *kartenAnsicht = [[Kundenkarte alloc]
initWithNibName:#"Kundenkarte" bundle:nil];
kartenAnsicht.rootViewController = self;
kartenAnsicht.viewDidLoad;
self.map = kartenAnsicht;
[self.view addSubview:kartenAnsicht.view];
[kartenAnsicht release];
// [super viewDidLoad];
}
The viewDidLoad that evokes the UIAlert is in the kartenAnsicht view controller.
I hope someone can help me because I'm out of ideas.
You don't need to call -viewDidLoad yourself, it's run automatically by the NIB-loading mechanism. That means you get extra invocations of -viewDidLoad: one by design, and extras whenever you call it.
First of all, you should never put any type of display in viewDidLoad. That method is intended for behind the scenes configuration after the view is first read from nib. There is no certainty that it will be called every time the view displays because after the first time it loads, the view maybe held in memory and not reloaded from nib.
Instead, put the call to evoke the NSAlert in viewWillDisplay or viewDidDisplay. This will display the alert each time the view appears.
I doubt that viewDidLoad is called three times but to check for that just put an NSLog in the method to see how many times it is called.
When you say that:
i implemented an NSAlert in the
viewDidLoad() of my app:
... what does that mean? What object exactly has the method? If it is the application delegate, this will not work because the application delegate protocol does not respond to viewDidLoad. It has to be in a UIViewController.
Edit01:
See this post that had the same problem: UIAlertView Pops Up Three Times per Call Instead of Just Once
Short answer: You kill the alert by releasing it. Either retain it as a property of the view controller or better yet, display the alert with runModal instead of show and capture the button number returned immediately.
It would be helpful to see the code around the alert call.
I am using an alert whenever the reachability changes. Since reachability is checked repeatedly, the alert could get called repeatedly. To alleviate that, I wrap the alert code like so:
if (!myAlert) { /* set up and show myAlert */ }
However, one problem with this is that when you click the Cancel button, the alert will remain non-nil, and therefore can never show again because of that conditional. If someone could add to this response with a fix for that, that would be great. I assume I can add a handler for the cancel button that will destroy myAlert.
If learned the hard way that you should remove the delegate from an object if the life span of the delegate is shorter than the object. But how do you do this if you don't have a reference to the object anymore?
In my iPhone application I have a view controller vc which performs an asynchronous activity and is displayed as a modal view. A cancel button dismisses the modal view. If an error occurs, an UIAlertView alert is displayed. If the user taps on ok, both alert and the modal view disappear. Therefore vc is set
as delegate for alert and implements alertView:didDismissWithButtonIndex:. Something like this:
// UIViewController vc
...
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Error"
message:#"Something went wrong"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
self.alertView = alert; // needed to unset alertView.delegate in dealloc
[alert show];
[alert release];
...
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
[self dismissModalViewControllerAnimated:YES];
}
}
Normally, the alert view blocks all input. Unfortunately, it fails to do so in some edge cases.
If the user touches down on the cancel-button before the alert view appears and touches up after the alert view appears, the view is dismissed, but not the alert. vc gets deallocated and if the user then taps "ok" on the alert, the application crashes because a message was sent to the released object.
I solved this by assigning alert to a property of vc so I can set alert.delegate to nil in dealloc. I find this solution to be not very elegant, because I don't really need the reference to alert.
Is there a better approach?
Edit: Added the text in italics as clarification
Although, normally an alert view is presented over non changing content. So if the delegate is alive when the view appears it will likely be alive when it's dismissed. If that's not the case, you have to do exactly what you did, and unset the alert view's delegate manually if you no longer care about it's result.
So you do care about the alertview since you care about it's delegate method. The wrinkle is that the delegate may not apply by the time the alert is dismissed. So you need logic there, and for that logic you need to save a reference to the alert view in question.
In other words, you are doing it right. Although, it may have been helpful if UIAlertView retained it's delegate, but it doesn't seem like it does if it crashes when it's dismissed.
Lastly, I thought alert view blocked all screen input? If not, you can make it truly modal by setting vc.view.userInteractionEnabled = NO when the alert appears and switch it back when it's dismissed. This way the user can't dismiss the controller while the alert view is up. Which sounds a bit more sane to me.
From a UI perspective, when an alert view is present, it should demand the user's full attention. Perhaps you might consider disabling the Cancel button (as well as any other visible non-alert-view widgets) when there is an alert view present, and adding a button title to the UIAlertView instance which performs the same task. This would provide a clearer user interface and should also neatly solve your memory issue.
-(void)delayedDismiss {
[self dismissModalViewControllerAnimated:YES];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
[self performSelector:#selector(delayedDismiss) withObject:nil afterDelay:0.0];
}
If you no longer care about the results of the UIAlertView you should probably also dismiss it in the - (void) viewWillDisappear:(BOOL)animated