【问题标题】:FileBrowser UITableViewController, Delegate doesn't workFileBrowser UITableViewController,委托不起作用
【发布时间】:2014-11-28 12:01:51
【问题描述】:

我决定使用 UITableViewController 创建一个简单的 FileBrowser,并使用委托检测您选择的文件,

我就这样展示了UITableViewController

fbVC = [[FileBrowserTableViewController alloc] init];
fbVC.delegate = self;
fbVC.path = @"/";
navigationController = [[UINavigationController alloc] initWithRootViewController:fbVC];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;

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

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

@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 方法在这里

- (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 中工作(一次)?特别是仅从第一个路径开始(如果我打开另一个路径并选择一个文件,它将不起作用。

谢谢

【问题讨论】:

    标签: ios objective-c uitableview delegates file-browser


    【解决方案1】:

    我想我自己找到了解决方案,我的错误在于didSelectRowAtIndexPath 方法 我没有再给代表打电话了。

    这里的代码对我有用

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

    再次感谢..希望对其他人有所帮助..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多