Dismiss actionSheet before action under it's button is completed - objective-c

My problem is that i have an ActionSheet, which is disappearing from screen only when action under this button is completed. My problem is that i want to click 'save' inside my action sheet, then dismiss action sheet and then to show some alert, informing user to wait until saving is done. Currently it works different: firstly action sheet is shown, then there is saving message UNDER action sheet, finally action sheet is removed from view.. so user doesnt see any alert message.
How to dismiss actionSheet earlier than xcode does it?
Method under sheetActionButton:
- (IBAction)saveAction:(id)sender
{
UIAlertView *alert;
alert = [[[UIAlertView alloc] initWithTitle:#"Saving photo to library\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease];
[alert show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
[indicator startAnimating];
[alert addSubview:indicator];
[indicator release];
[self saveImageToCameraRoll];
[alert dismissWithClickedButtonIndex:0 animated:YES];
}

You should move your saveImageToCameraRoll method onto a separate thread, or at least asynchronously on the main thread. Then you can dismiss the alert and saveAction: can return before it completes.
The simplest way to do this would be using dispatch_async. Use dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) to get a queue for a separate thread, or dispatch_get_main_queue() for the main thread. Make sure not to do any UI work (or use any APIs which aren't thread-safe) on other threads!
Edit: more detail:
- (IBAction)saveAction:(id)sender {
UIAlertView *alert;
alert = [[[UIAlertView alloc] initWithTitle:#"Saving photo to library\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease];
[alert show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
[indicator startAnimating];
[alert addSubview:indicator];
[indicator release];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Save in the background
[self saveImageToCameraRoll];
dispatch_async(dispatch_get_main_queue(), ^{
// Perform UI functions on the main thread!
[alert dismissWithClickedButtonIndex:0 animated:YES];
});
});
}

Related

Why error is showing on click of button?

Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates when I am using UIActionsheet on click of button in ios? Why?
- (IBAction)btnAboutUsActions:(UIButton *)sender
{
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: nil
delegate:self
cancelButtonTitle: nil
destructiveButtonTitle: nil
otherButtonTitles: #"Achievment",
#"Brown Story", nil];
actionSheet.tag = 100;
[actionSheet showFromRect: sender.frame inView: sender.superview animated: YES];
}
Use alertController
UIAlertController *alert = [UIAlertController alertControllerWithTitle:confirmText message:#"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *achievment = [UIAlertAction actionWithTitle:#"Achievment"
style:UIAlertActionStyleDefault
handler:nil];
[alert addAction: achievment];
[self presentViewController:alert animated:YES completion:nil];
[alert.view layoutIfNeeded]

UIAlertView does not work with UIBarButton

I have a strange problem:
Trying to call an action which contains NSLog and UIAlert using a UIBarButtonItem from a NavigationController. NSLog prints and UIAlert doesn't appear…
-(void)addNewItem
{
UIAlertView *alertAddnewItem = [[UIAlertView alloc]initWithTitle:#"Hello!" message:#"hello!" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
[self.view addSubview:alertAddnewItem];
NSLog(#"Hello!");
}
I guess the problems is with [self.view addSubview:alertAddnewItem];
Thank you!
call:
[alertAddnewItem show];
not
[self.view addSubview:alertAddnewItem];

Code breakpoint

Hi I have a code that when I run quits and says there is a breakpoint
- (void)checkCollision{
if(CGRectIntersectsRect(penny.frame, hand.frame))
{
[randomMain invalidate];
[startButton setHidden:NO];
pos= CGPointMake(0.0, 0.0);
CGRect frame = [penny frame];
frame.origin.x=137.0f;
frame.origin.y=326.0;
[penny setFrame:frame]; (the breakpoint is here)
CGRect frame2 = [hand frame];
frame2.origin.x=137.0f;
frame2.origin.y=20.0;
[hand setFrame:frame2];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"You Lose" message:[NSString stringWithFormat:#"He got the penny!"] delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
any ideas?
i apologize for the sloppy format Im new to the website, thanks!
Sometimes a breakpoint gets out of sync with the code presented. Try going to your breakpoint window (Command + 6 or View->Navigators->Show Breakpoint Navigator.
In that navigator, you may see active breakpoints. If you can find the errant breakpoint, delete it from the list. Otherwise, your best bet is to delete all your breakpoints.

Is it possible to create an UIAlertView without any buttons?

I need help with UIAlertView :-). Currently I have an UIAlertView showing up when the user shakes the device using the -(void)motionEnded: function. I want to make the alert view disappear after 0.5 seconds using a NSTimer so I don't need any button at the bottom of the alert view to dismiss it. Is there a way to create an UIAlertView without any button? [A way to remove the space with the arrow in the image below?]
Here's the code:
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
resetAlert = [[UIAlertView alloc] initWithTitle:#"Reset!"
message:nil
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil, nil];
[resetAlert show];
alertHideTimer = [NSTimer scheduledTimerWithTimeInterval:10.5 target:self selector:#selector(dismissWithClickedButtonIndex:animated:) userInfo:nil repeats:NO];
self.label.text = #"0";
numero = 0;
[self.label2 setHidden:YES];
}
Yes it is. Try the following: iToast
You can create your own view. Show and hide/close it as per your requirement, timers etc.
I'm not sure if having the NSTimer with a multiple argument selector is correct. I add this method:
-(void)dismissAlertView:(UIAlertView*)alertView {
[alertView dismissWithClickedButtonIndex:0 animated:NO];
}
and replace your NSTimer with:
[self performSelector:#selector(dismissAlertView:) withObject:resetAlert afterDelay:0.5];
As far as I know removing the space entirely is not possible.
However... here are some things you may want to try;
Adding top padding
Add a few newlines to get top padding: initWithTitle:#"\n\nConfiguring Preferences...
(source: iosdevelopertips.com)
Adding a spinner
Add a UIActivityIndicatorView to the UIAlertView
alert = [[UIAlertView alloc] initWithTitle:#"Configuring Preferences\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
Adjust the indicator so it is up a few pixels from the bottom of the alert
indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
[indicator startAnimating];
[alert addSubview:indicator];
(source: iosdevelopertips.com)
Hope that helps.
You can hide the alert simply by calling the delegate method after 5 sec
[alert dismissWithClickedButtonIndex:0 animated:YES];

UIAlertView causes crash in Release mode

If the Help option is activated in my app when the user goes to the camera I show a UIAlertView first with tips on how to take a picture:
-(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [actionSheet cancelButtonIndex]) {
NSString *selectedButtonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([selectedButtonTitle isEqualToString:#"Camera"]) {
// If Help is activated display camera tips
if (helpEnabled == YES) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Camera Tips" message:#"\n\n\n\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Go To Camera"];
UIImageView *cameraHelpView = [[UIImageView alloc] initWithFrame:CGRectMake(17, 40, 250, 255)];
UIImage *cameraTutorial = [UIImage imageNamed:#"Camera_Tips.png"];
cameraHelpView.image = cameraTutorial;
[alert addSubview:cameraHelpView];
[cameraHelpView release];
[alert show];
[alert release];
}
}
}
This works in Debug mode but causes an "EXC BAD ACCESS" error in Release mode. I can present a new view controller modally from this point just fine, however the UIAlertView will always crash the app. Why?
I don't know why it works in debug mode, but it looks like you are releasing your cameraHelpView while it is still being used. In the subviews of alert is a pointer to cameraHelpView; when you release that, it can no longer be accessed. I would suggest replacing all of your -[NSObject release] calls in this context with -[NSObject autorelease]. Thus:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Camera Tips" message:#"\n\n\n\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Go To Camera"] autorelease];
UIImage *cameraTutorial = [UIImage imageNamed:#"Camera_Tips.png"];
UIImageView *cameraHelpView = [[[UIImageView alloc] initWithFrame:CGRectMake(17, 40, 250, 255)] autorelease];
cameraHelpView.image = cameraTutorial;
[alert addSubview:cameraHelpView];
[alert show];
Go ahead and try that and let me know if it works. Good luck!
I found my mistake. I wasn't passing nil as the final argument to otherButtonTitles! Debug mode must see and fix this error for you. Hope this helps someone.