2013-04-01 27 views
2

所以我試圖爲我的ObjectiveC應用實現相機功能。我有一個MasterViewController有一個tableviewDetailViewController處理查看cells的詳細信息並添加新的信息。我在我的navbar上有一個相機按鈕,我想打開系統相機的東西,並能夠從那裏抓取圖像。xcode在嘗試實現相機功能時拋出「DetailViewController *」給不兼容類型警告的準備工作

唯一的問題,我的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 
+0

顯示您的.h文件DetailViewController – borrrden

+0

請不要發佈這麼多的代碼。顯示提供錯誤的代碼。並顯示類接口聲明。 – rmaddy

回答

2

最有可能你不聲明你的類符合UIImagePickerControllerDelegate協議。

@interface DetailViewController() 

到:通過改變這一行更新您的m

@interface DetailViewController() <UIImagePickerControllerDelegate> 

有些人可能在.h文件,但除非其他類將其添加到@interface線需要知道您的視圖控制器符合委託協議,最好將它添加到.m文件中的類擴展名中。

此外,下面的代碼:

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

應該僅僅是:

imagePicker.delegate = self; 

更新:現在你已經張貼您的.H,請注意,你不應該聲明UIImagePickerControllerDelegate方法在你的.h文件中作爲類接口的一部分。添加協議爲您做到這一點。從您的.h文件中刪除這些:

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

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

您絕對不想在私有界面中添加委託聲明,您想將其添加到.h文件中的公共界面。 – eyebrowsoffire

+0

@eyebrowsoffire爲什麼?我儘可能多地使用.m。它工作得很好。沒有理由告訴世界,一個類的實現要求它符合一些協議。 – rmaddy

+0

除了調用委託方法的控件不限於此文件... – eyebrowsoffire

相關問題