Search code examples
objective-ciosuibarbuttonitemuiactionsheet

UIActionSheet: handle the call of methods in a right chronology


The Context
In my App there are two UIBarButtonItems. If I click on the first button called save-button, a UIActionSheet appears and ask me to save. When I accept the save process, the actual image should be saved in the library.

When I click to the second button called delete-button, the same request via UIActionSheet should start. After accepting the operation the image should be deleted.

For this I have two methods of IBAction and one for the UIActionSheet.

Implementation of the methods

-(IBAction)save: (id) sender{
UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle: @"Sure to save?"delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: @"Save",@"Cancel",nil];
    actionSheet.tag = 100;
    [actionSheet showInView:self.view];
    [actionSheet release];
}
-(IBAction)bin: (id) sender{
UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:@"Sure to delete?"delegate: self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Delete",@"Cancel",nil];
    actionSheet.tag = 101;
    [actionSheet showInView:self.view];
    [actionSheet release];
}

By reason that "willPresentActionSheet" could not implement two times in a class, I use the tags to handle the save- and the delete button.

-(void)willPresentActionSheet:(UIActionSheet*)actionSheet{
   if (actionSheet.tag == 100) {
        CGRect contextRect  = CGRectMake(0, 960, 768, 1004);
        UIGraphicsBeginImageContext(contextRect.size);  

    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image1 = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(image1, nil, nil, nil);

} else if (actionSheet.tag == 101) {
    imageView.image = nil; 

    NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Default.png"];  
        [imageView setImage:[UIImage imageWithContentsOfFile:filePath]];
    } 
}

The Problem
When I press the delete-Button the actionSheet appears, but in the same time (before I allow to delete) the image is deleted yet.

What is wrong or what did I miss in my methods? If there is a lack of clarity concerning my app or my problem, do not shy away from asking.

Thank you for your help in advance


Solution

  • It sounds like you are attempting to process an action after the user clicks one of the option buttons in an action sheet. However, the function willPresentActionSheet will be called before the action sheet is even presented. If you are trying to delete the image only after the user has clicked to confirm it in the action sheet, take a look at the - clickedButtonAtIndex function in the UIActionSheetDelegate.

    (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    

    Be sure to assign the delegate of the UIActionSheet to the class that is handling the action sheet call back functions (most likely - "self" as you show it above).

    In the clickedButtonAtIndex handler, you can still use the tag to differentiate UIActionSheets and use the index of the button to determine which was clicked.