iPhone UIDocumentInteractionController presentOpenInMenuFromRect取消按钮不响应

时间:2011-08-10 17:29:55

标签: iphone objective-c ipad sdk

我在iPhone4和iPad2中执行“presentOpenInMenuFromRect”时观察到不同的行为(两者都在iOS 4.3.5中运行)。在iPad2中,它会显示一个下拉列表,其中包含可以打开特定文件的所有应用程序,并且工作正常;但是,在iPhone4中它也会显示一个下拉列表(也可以正常工作),但是在下拉列表的末尾有一个“取消”按钮,它似乎处于非活动状态。

在iPad2中,这个问题不会发生,因为没有出现“取消”按钮,只出现下拉列表,当我点击与下拉列表不同的另一个区域时,此列表关闭;这是理想的行为。

我的意思是说iPhone / iPhone4中的“取消”按钮似乎处于非活动状态:当我触摸它时,没有任何反应!好吧,更具体地说,如果我在短时间内或时间内多次触摸它,那么,比较早,按钮“取消”似乎响应(它将颜色从灰色变为蓝色)然后下拉列表真的很封闭;这是理想的行为。

我通过以下方式使用“presentOpenInMenuFromRect”:我已经实现了一个“保存”按钮,默认情况下是隐藏的;但是,在方法“ - (void)webViewDidFinishLoad:(UIWebView *)webView”我检测到URL的“路径扩展”,如果它有“.pdf”扩展名,那么我显示“保存”按钮;那就是:

-  (void)webViewDidFinishLoad:(UIWebView *)webView
{
    if ([[[myWebViewOL.request.URL.absoluteString pathExtension] lowercaseString] isEqualToString:@"pdf"])
    {
        saveFile0L.hidden = NO;
    }
}

与“保存”按钮(saveFile0L)相关的“动作”是以下方法“saveFile”:

[saveFile0L addTarget:self action:@selector(saveFile) forControlEvents:UIControlEventTouchUpInside];

其中“saveFile”具有以下代码:

- (void) saveFile
{
    //Do not worry about "FileManager" is only a singleton class
    //which I have defined in order to implement some common methods
    //and one of them is "saveFile:(UIWebView*) withUIView:(UIView*)
    [[FileManager sharedInstance] saveFile:myWebView withUIView:self.view];
}

并且“saveFile:(UIWebView *)myWebView withUIView:(UIView *)self.view”具有以下代码:

- (void) saveFile:(UIWebView*)webView withUIView:(UIView*)view
{
    NSString* fileName  = [[NSFileManager defaultManager] displayNameAtPath:webView.request.URL.absoluteString];
    NSURL* fileurl = [NSURL URLWithString:webView.request.URL.absoluteString];
    NSData* data = [NSData dataWithContentsOfURL:fileurl];
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* docsDirectory = [paths objectAtIndex:0];
    NSString* filePath = [docsDirectory stringByAppendingPathComponent:fileName];
    [data writeToFile:filePath atomically:YES];
    NSURL* url = [NSURL fileURLWithPath:filePath];
    //UIDocInteractionController API gets the list of devices that support the file type
    docController = [UIDocumentInteractionController interactionControllerWithURL:url];
    [docController retain];
    //Note:[docController presentOpenInMenuFromRect:CGRectZero inView:view animated:YES]
    //presents a drop down list of the apps that support the file type,
    //clicking on an item in the list will open that app while passing in the file.
    //Note: [[UIApplication sharedApplication] canOpenURL:fileurl]
    //will return YES if there is an app that can handle the specific file
    BOOL isValid = ([[UIApplication sharedApplication] canOpenURL:fileurl] &&
                    [docController presentOpenInMenuFromRect:CGRectZero inView:view animated:YES]);
    if (!isValid)
    {
        [self showAlertSaveFileError:fileName];
    }
}

目前,我不知道如何正确使用“取消”按钮,该按钮仅出现在下拉列表末尾的iPhone(不在iPad中)中。

提前致谢,

2 个答案:

答案 0 :(得分:8)

BOOL isValid = ([[UIApplication sharedApplication] canOpenURL:fileurl] &&
                    [docController presentOpenInMenuFromRect:CGRectZero inView:view animated:YES]);

尝试将view inView:view更改为inView:self.view.window

答案 1 :(得分:0)

这是我的错;最后我可以解决问题。

错误是使用“self.view”并将其作为函数“presentOpenInMenuFromRect”的参数传递

[docController presentOpenInMenuFromRect:CGRectZero inView:view animated:YES]

然而,这个“观点”(self.view)并不适合处理触摸事件;由于以下警告,可以注意到这个错误,该警告显示在Xcode控制台中:

2011-08-17 18:12:56.389 MyApplication[287:707] Presenting action sheet clipped by its superview. Some controls might not respond to touches. On iPhone try -[UIActionSheet showFromTabBar:] or -[UIActionSheet showFromToolbar:] instead of -[UIActionSheet showInView:].

这条消息给了我线索,我已经按照以下方式修改了代码:

新的和正确的代码是:

- (void) saveFile:(UIWebView*)webView
{
    NSString* fileName  = [[NSFileManager defaultManager]  displayNameAtPath:webView.request.URL.absoluteString];
    #if DEBUG
    NSLog(@"<%p %@: %s line:%d> File name:%@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __PRETTY_FUNCTION__, __LINE__, fileName);
    #endif
    NSURL* fileurl = [NSURL URLWithString:webView.request.URL.absoluteString];
    NSData* data = [NSData dataWithContentsOfURL:fileurl];
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* docsDirectory = [paths objectAtIndex:0];
    NSString* filePath = [docsDirectory stringByAppendingPathComponent:fileName];
    [data writeToFile:filePath atomically:YES];
    NSURL* url = [NSURL fileURLWithPath:filePath];
    //UIDocInteractionController API gets the list of devices that support the file type
    docController = [UIDocumentInteractionController interactionControllerWithURL:url];
    [docController retain]; //Very important, if "retain" is not called, the application crashes
    //Present a drop down list of the apps that support the file type,
    //clicking on an item in the list will open that app while passing in the file.
    BOOL isValid = [docController presentOpenInMenuFromRect:CGRectZero inView:webView animated:YES]; //Using "webView" instead of "self.view"
    if (!isValid)
    {
        [self showAlertSaveFileError:fileName]; //Shows an alert message
    }
}

也就是说,传递给方法“presentOpenInMenuFromRect:inView:animated:”视图“webView”(这是一个UIWebView),而不是“self.view”。