所以我試圖爲我的ObjectiveC應用實現相機功能。我有一個MasterViewController
有一個tableview
。 DetailViewController
處理查看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
顯示您的.h文件DetailViewController – borrrden
請不要發佈這麼多的代碼。顯示提供錯誤的代碼。並顯示類接口聲明。 – rmaddy