在尝试实现相机功能时,xcode将“DetailViewController *”抛给不兼容类型警告的参数

时间:2013-04-01 04:26:13

标签: ios objective-c uiimagepickercontroller

所以我正在尝试为我的ObjectiveC应用程序实现相机功能。我的MasterViewController有一个tableviewDetailViewController处理查看cells的详细信息并添加新信息。我的navbar上有一个相机按钮,我想打开系统相机的东西,并能从那里抓取图像。

唯一的问题是,由于某种原因,我的UIImagePickerController不允许我将DetailViewController设为代理人。它只是在这篇文章的标题中抛出警告。我确保委托得到妥善实施。一篇类似的帖子也建议同时实施navigationcontrollerdelegate,所以我就这样做了。

按照目前的情况,我认为在我可以正确设置委托之前相机无法工作。我在这里附上了相关课程的代码:

#import "DetailViewController.h"
#import "MasterViewController.h"
#import "BucketItem.h"

@interface DetailViewController ()
@property (strong, nonatomic) UIPopoverController *masterPopoverController;
- (void)configureView;
@end

@implementation DetailViewController

- (void)dealloc
{
    [_detailItem release];
    [_masterPopoverController release];
    [super dealloc];
}

#pragma mark - Managing the detail item

-(void)setCellForEditing:(UITableViewCell*)cell
{
    cellBeingEdited = cell;
}

- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        [_detailItem release];
        _detailItem = [newDetailItem retain];

        // Update the view.
        [self configureView];
    }

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

- (void)configureView
{
    // Update the user interface for the detail item.
    if(cellBeingEdited != nil)
    {
        NSUInteger cellNumber = [self.data.bucketItemCells indexOfObject:cellBeingEdited];
        BucketItem *item = [self.data.bucketItems objectAtIndex:cellNumber];
        titleInputBox.text = item.title;
        descriptionInput.text = item.description;
        isCompletedSwitch.on = item.isCompleted;
        date = item.date;
        imageView.image = item.image;
    }
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self configureView];
    UIBarButtonItem * doneButton =[[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target:self action:@selector(saveObject:)] autorelease];
    UIBarButtonItem * cameraButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCamera target:self action:@selector(takePicture:)] autorelease];
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects: doneButton, cameraButton, nil];

}

-(void) takePicture:(id)sender
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
    }
    else
    {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    }
    DetailViewController *tmp = self;
    [imagePicker setDelegate: tmp];

}

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    [imageView setImage:image];
    [self dismissViewControllerAnimated:NO completion:nil];
}

-(void) saveObject:(id)sender
{

    UIImage *image = nil;
    if(imageView.image != nil)
    {
        image = imageView.image;
    }
    if(cellBeingEdited == nil)
    {
        [self.data addNewItemWithTitle: titleInputBox.text
                        andDescription: descriptionInput.text
                       thatIsCompleted: isCompletedSwitch.on
                              withDate: date
                             withImage: image];

    }
    else
    {
        NSUInteger cellNumber = [self.data.bucketItemCells indexOfObject:cellBeingEdited];
        BucketItem *item = [self.data.bucketItems objectAtIndex:cellNumber];
        item.title = titleInputBox.text;
        item.description = descriptionInput.text;
        item.isCompleted = isCompletedSwitch.on;
        item.date = date;
        item.image = image;
        [self.data.bucketItemCells replaceObjectAtIndex:cellNumber withObject:[self.data cellWithBucketItem:item]];
    }
    MasterViewController *vc = [self.navigationController.viewControllers objectAtIndex:0];
    [vc.tv reloadData];
    [self.navigationController popViewControllerAnimated:YES];

}

-(IBAction) onSwitchToggle:(id)sender
{
    if(isCompletedSwitch.on == YES)
    {
        date = [[NSDate dateWithTimeIntervalSince1970:1.0] retain];
        NSDateFormatter *format = [[NSDateFormatter alloc] init];
        [format setDateFormat: @"yyyy-MM-dd"];
        dateLabel.text =[format stringFromDate: date];
        [format release];
    }
    else
    {
        date = nil;
    }
}

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

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Detail", @"Detail");
    }
    return self;
}

-(BOOL)textFieldShouldReturn:(UITextField* )aTextField
{
    [aTextField resignFirstResponder];
    return YES;
}


#pragma mark - Split view

- (void) navigationController: (UINavigationController *) navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
}

-(void) navigationController: (UINavigationController *) navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
}

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

}



- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
    barButtonItem.title = NSLocalizedString(@"Master", @"Master");
    [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
    self.masterPopoverController = popoverController;
}

- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    // Called when the view is shown again in the split view, invalidating the button and popover controller.
    [self.navigationItem setLeftBarButtonItem:nil animated:YES];
    self.masterPopoverController = nil;
}
@end

这是所要求的.h文件:

@interface DetailViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UISplitViewControllerDelegate, UITextFieldDelegate>
{
    IBOutlet UITextField *titleInputBox;
    IBOutlet UITextField *descriptionInput;
    IBOutlet UISwitch *isCompletedSwitch;
    IBOutlet UILabel *dateLabel;
    IBOutlet UIImageView *imageView;
    NSDate *date;
    UITableViewCell *cellBeingEdited;
}


@property (strong, nonatomic) id detailItem;
@property (readwrite, assign) BucketListDataSource *data;

-(IBAction) onSwitchToggle:(id)sender;

-(void)setCellForEditing:(UITableViewCell*)cell;

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;

-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker;
@end

1 个答案:

答案 0 :(得分:2)

您很可能没有声明您的班级符合UIImagePickerControllerDelegate协议。通过更改此行来更新.m:

@interface DetailViewController ()

为:

@interface DetailViewController () <UIImagePickerControllerDelegate>

有些人可能会将此添加到.h文件中的@interface行,但除非其他类需要知道您的视图控制器符合委托协议,否则最好将其添加到类扩展中。 .m文件。

另外,以下代码:

DetailViewController *tmp = self;
[imagePicker setDelegate: tmp];

应该是:

imagePicker.delegate = self;

更新:现在您已经发布了.h,请注意,您不应将.h文件中的UIImagePickerControllerDelegate方法声明为类接口的一部分。添加协议会为您完成此操作。从你的.h文件中删除它们:

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;

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