首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FileBrowser UITableViewController,委派不工作

FileBrowser UITableViewController,委派不工作
EN

Stack Overflow用户
提问于 2014-11-28 20:01:51
回答 1查看 70关注 0票数 0

我决定使用UITableViewController和delegate创建一个简单的FileBrowser来检测你选择的文件,

我就像这样展示了UITableViewController

代码语言:javascript
复制
fbVC = [[FileBrowserTableViewController alloc] init];
fbVC.delegate = self;
fbVC.path = @"/";
navigationController = [[UINavigationController alloc] initWithRootViewController:fbVC];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;

并在另一个类中添加了委托方法

代码语言:javascript
复制
- (void)fileBrowser:(FileBrowserTableViewController *)fileBrowser didFinishWithFileURL:(NSURL *)fileURLPath {
    NSString *extString = [fileURLPath absoluteString];
    NSString *ext = [[extString pathExtension] lowercaseString];
    NSString* theFileName = [[extString lastPathComponent] stringByDeletingPathExtension];

    CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)fileURLPath.pathExtension, NULL);
    CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType);
    NSString *MIMETypeString = (__bridge NSString*)MIMEType;

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"FileBrowser" message:[NSString stringWithFormat:@"---URL : %@ --FileName : %@ --ext %@: mimetype : %@", fileURLPath, theFileName, fileURLPath.pathExtension, MIMETypeString] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
    [self.companion sendDocumentsAtPath:fileURLPath fileName:[NSString stringWithFormat:@"[TGEnhancer]%@.%@",theFileName, fileURLPath.pathExtension] mimeType:MIMETypeString];
    // [fileBrowser.navigationController popViewControllerAnimated:YES];
    [fileBrowser.navigationController dismissViewControllerAnimated:YES completion:^{
        NSLog(@"File Browser - Finished");
    }];
}

但是由于某些原因,这个委托只能从UINavigationController的第一个页面开始工作,这里是我的.h文件

代码语言:javascript
复制
@protocol FileBrowserTableViewControllerDelegate;

@interface FileBrowserTableViewController : UITableViewController 
{
    NSString        *path;
    NSMutableArray  *files;
}

@property (nonatomic,retain) NSString       *path;
@property (nonatomic,retain) NSMutableArray *files;
@property (nonatomic, strong) id<FileBrowserTableViewControllerDelegate> delegate;

@end

@protocol FileBrowserTableViewControllerDelegate <NSObject>
@optional
- (void)fileBrowser:(FileBrowserTableViewController *)fileBrowser didFinishWithFileURL:(NSURL *)fileURLPath;
- (void)fileBrowserDidCancel:(FileBrowserTableViewController *)fileBrowser;

@end

这里是我的didSelectRowAtIndexPath方法

代码语言:javascript
复制
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    File *aFile = [files objectAtIndex:indexPath.row];
    if(aFile.isDirectory)
    {
        FileBrowserTableViewController *anotherViewController = [[FileBrowserTableViewController alloc] init];
        anotherViewController.path = [path stringByAppendingPathComponent:aFile.name];
        [self.navigationController pushViewController:anotherViewController animated:YES];
    } else {
        [self doOpenFileAtIndexPath:indexPath];
    }
}

- (void)doOpenFileAtIndexPath:(NSIndexPath*)indexPath {
    // File *aFile = [files objectAtIndex:indexPath.row];
    [self openFileAtIndexPath:indexPath];
}

- (void)openFileAtIndexPath:(NSIndexPath*)indexPath
{           
    File *aFile = [files objectAtIndex:indexPath.row];
    NSString *extension = [[aFile.name pathExtension] lowercaseString];
    NSString *fullpath = [path stringByAppendingPathComponent:aFile.name];
    NSURL *filePathUrl = [NSURL fileURLWithPath:fullpath];
        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Type Existe"
                                                                 message:[NSString stringWithFormat:@"--Name : %@ Fullpath : %@", aFile.name, fullpath]
                                                                delegate:nil
                                                       cancelButtonTitle:@"OK"
                                                       otherButtonTitles:nil];
        [alertView show];
    [self.delegate fileBrowser:self didFinishWithFileURL:filePathUrl];
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

有谁可以帮助我,告诉我在UINavigationController中到底是什么让委托工作(一次)?特别是从第一个路径(如果我打开另一个路径并选择一个文件,它将不起作用。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2014-11-28 20:19:06

OPs我想我自己找到了解决方案,我的错误是在didSelectRowAtIndexPath方法中,我没有再次调用委托。

这里的代码为我工作

代码语言:javascript
复制
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    File *aFile = [files objectAtIndex:indexPath.row];
    if(aFile.isDirectory)
    {
        FileBrowserTableViewController *anotherViewController = [[FileBrowserTableViewController alloc] init];
        anotherViewController.path = [path stringByAppendingPathComponent:aFile.name];
        anotherViewController.delegate = self.delegate;
        [self.navigationController pushViewController:anotherViewController animated:YES];
    } else {
        [self doOpenFileAtIndexPath:indexPath];
    }
}

再次感谢..希望它能帮助其他人..

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27188235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档