将数据从详细信息视图传递到主视图(Splitviewcontroller)

时间:2013-12-19 08:02:52

标签: ios image ipad uisplitviewcontroller exif

我是iOS编程的新手,我对Splitviewcontroller有疑问。

我正在编写一个iPad应用程序,它从图库中加载图片,然后将EXIF数据保存到表格视图中。加载图片没问题,但是当我试图将EXIF数据保存到表视图中时,没有任何反应。我有适用于iPhone的应用程序,但没有使用Splitview,一切正常。

我现在搜索了几天的答案,但对我的问题没有任何帮助。

以下是一些代码:

ViewController.m

#import "ViewController-iPad.h"
#import "TableController-iPad.h"

@implementation ViewController_iPad
{
    UIPopoverController *masterPopoverController;
}

@synthesize exifPtr;
@synthesize toolbar;
@synthesize imageView;
@synthesize detailItem = _detailItem;

-(void) setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem)
    {
        _detailItem = newDetailItem;
    }

    if (masterPopoverController != nil)
    {
        [masterPopoverController dismissPopoverAnimated: YES];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {

        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                              message:@"Device has no camera"
                                                             delegate:nil
                                                    cancelButtonTitle:@"OK"
                                                    otherButtonTitles: nil];

        [myAlertView show];

    }
}

- (void)viewDidUnload
{
    [self setToolbar:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (IBAction)selectPhoto:(UIButton *)sender
{

   UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:picker animated:YES completion:NULL];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"exifCell"])
    {
        TableController_iPad *controller = (TableController_iPad *)segue.destinationViewController;
        controller.exifPtr = exifPtr;
    }
}

+ (ALAssetsLibrary *)defaultAssetsLibrary
{
    static dispatch_once_t pred = 0;
    static ALAssetsLibrary *library = nil;
    dispatch_once(&pred, ^{
        library = [[ALAssetsLibrary alloc] init];
    });
    return library;
}

#pragma mark - Image Picker Controller delegate methods

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = chosenImage;

    NSURL *url = info[UIImagePickerControllerReferenceURL];

    ALAssetsLibrary *lib = [ViewController_iPad defaultAssetsLibrary];
    [lib assetForURL:url resultBlock:^(ALAsset *asset)
    {

         // get data
         ALAssetRepresentation *repr = [asset defaultRepresentation];

         long long bytes = [repr size];
         unsigned char *dataPtr = malloc((unsigned long)bytes);
         [repr getBytes:dataPtr fromOffset:0 length:(NSUInteger)bytes error:NULL];

         JPEG_GetEXIFFromMemory(dataPtr, (long)bytes, &exifPtr);
         PrvConvertEXIFToText(exifPtr);

         free(dataPtr);

     } failureBlock:^(NSError *error)
     {
         ;
     }];

    [picker dismissViewControllerAnimated:YES completion:NULL];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker    
{

    [picker dismissViewControllerAnimated:YES completion:NULL];

}

// Standard-Methoden für einen SplitViewController
-(void) splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc
{
    barButtonItem.title = @"EXIF";
    self.navigationItem.rightBarButtonItem = barButtonItem;
    NSMutableArray *items = [[self.toolbar items] mutableCopy];
    [items insertObject: barButtonItem atIndex: 0];
    [self.toolbar setItems:items animated:YES];
    masterPopoverController = pc;
}

-(void) splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    NSMutableArray *items = [[self.toolbar items] mutableCopy];
    [items removeObject:barButtonItem];
    [self.toolbar setItems:items animated:YES];
    masterPopoverController = nil;
}

@end

TableController.m

#import "TableController-iPad.h"

@interface TableController_iPad()

@property (nonatomic, strong) NSArray *exifTitleArray;
@property (nonatomic, strong) NSArray *exifContentArray;

@end

@implementation TableController_iPad

@synthesize exifTitle;
@synthesize exifContent;
@synthesize exifPtr;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    cellTitle = [NSArray arrayWithObjects: exifTitle, nil];
    cellContent = [NSArray arrayWithObjects: exifContent, nil];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(exifPtr==0)
    {
        return 0;
    }
    return exifPtr->textCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //NSString *simpleTableIdentifier = [NSString stringWithFormat:@"exifCell %ld", (long)indexPath.row];

    static NSString *simpleTableIdentifier = @"exifCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:simpleTableIdentifier];
    }

    // Configure the cell...

    char * exifDataTitle = exifPtr->exifTextRec[indexPath.row].description;
    NSString * exifCStringTitle = [NSString stringWithCString: exifDataTitle encoding:NSUTF8StringEncoding];

    char * exifDataContent = exifPtr->exifTextRec[indexPath.row].content;
    NSString * exifCStringContent = [NSString stringWithCString: exifDataContent encoding:NSUTF8StringEncoding];

    cell.textLabel.font = [UIFont systemFontOfSize:10.0];
    cell.textLabel.text = exifCStringTitle;
    cell.detailTextLabel.font = [UIFont systemFontOfSize:10.0];
    cell.detailTextLabel.text = exifCStringContent;

    return cell;
}
@end

PS:抱歉有些语法错误,英语不是我最好的语言;)

1 个答案:

答案 0 :(得分:0)

您可以使用自定义委托将数据从子视图控制器获取到父视图控制器。

This链接可以帮助您实现这一目标。