UIActionSheet:按正确的时间顺序处理方法的调用

发布于 2024-12-19 00:06:07 字数 1893 浏览 2 评论 0原文

背景
在我的应用程序中有两个 UIBarButtonItems。 如果我单击第一个名为“保存”按钮的按钮,则会出现一个 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”无法在一个类中实现两次,我使用标签来处理保存和删除按钮。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们的影子 2024-12-26 00:06:07

听起来好像您正在尝试在用户单击操作表中的选项按钮之一后处理操作。然而,函数 willPresentActionSheet 将在操作表呈现之前被调用。如果您仅在用户在操作表中单击确认后才尝试删除图像,请查看 UIActionSheetDelegate 中的 -clickedButtonAtIndex 函数。

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

请务必将 UIActionSheet 的委托分配给处理操作表回调函数的类(很可能是“self”,如上面所示)。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文