所以我正在尝试为我的 ObjectiveC 应用程序实现相机功能。我有一个MasterViewController
有一个tableview
。DetailViewController
句柄查看详细信息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