UIActionSheet:处理正确年表中的方法调用

问题描述:

上下文
在我的应用程序中有两个UIBarButtonItems。 如果我点击名为保存按钮的第一个按钮,出现一个UIActionSheet并要求我保存。当我接受保存过程时,实际图像应保存在库中。UIActionSheet:处理正确年表中的方法调用

当我点击第二个按钮叫做删除按钮,通过UIActionSheet应该开始相同的请求。接受操作后,图像应该被删除。

为此我有两种方法IBAction和一个为UIActionSheet

的方法

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

通过的原因,“willPresentActionSheet”无法在一个类中实现两次实施,我使用标签来处理save-和删除按钮。

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

的问题
当我按下删除键式的actionSheet出现,但在相同的时间图像尚未删除(之前我允许删除)。

什么是错,或者我在方法中错过了什么? 如果我的应用程序或我的问题缺乏清晰度,请不要回避问题。

感谢您的帮助提前

+0

是不是因为你在调用imageView.image = nil;在第一行? – Novarg

+0

是的,你是对的。但点击按钮“删除”后是不是可以删除图像? – Studie

这听起来像你正试图在用户单击动作片的一个选项按钮,后处理操作。但是,在操作表出现之前,函数willPresentActionSheet将被调用。如果您只是在用户单击确认后才删除图像,请查看UIActionSheetDelegate中的 - clickedButtonAtIndex函数。

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

务必将UIActionSheet的代表分配给正在处理的动作片回呼功能(最有可能的 - “自我”为你展示它上面)的类。

在clickedButtonAtIndex处理程序中,仍然可以使用标记区分UIActionSheets并使用按钮的索引确定哪个被单击。