FileBrowser UITableViewController, Delegate doesn't work

85 views Asked by At

i decided to create a simple FileBrowser using UITableViewController with delegate which detect the file you chosen,

and i presented the UITableViewController like that

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

and added the delegate method in the other class

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

but for some reason the delegate works only from the first page of UINavigationController here is my .h file

@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

my didSelectRowAtIndexPath method here

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

anyone can help me and tell me What exactly make delegate works (once) within UINavigationController ? specially from the first path only ( if i open another path and choose a file, it won't work.

Thanks

1

There are 1 answers

0
iMokhles On

OPs i think i find the solution by myself, my mistake was in didSelectRowAtIndexPath method i wasn't calling the delegate again.

here the code worked for me

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

Thanks again.. hope it helps others..