gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:14:48 25 4
gpt4 key购买 nike

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

唯一的问题是,出于某种原因,我的 UIImagePickerController 不允许我将我的 DetailViewController 设置为委托(delegate)。它只是在这篇文章的标题中发出警告。我已确保正确实现委托(delegate)。一篇类似的帖子建议也实现 navigationcontrollerdelegate,所以我照做了。

就目前而言,我认为在我可以正确设置委托(delegate)之前相机将无法工作。我在此处附上了相关类(class)的代码:

#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

最佳答案

很可能您没有声明您的类符合 UIImagePickerControllerDelegate 协议(protocol)。通过更改此行更新您的 .m:

@interface DetailViewController ()

到:

@interface DetailViewController () <UIImagePickerControllerDelegate>

有些人可能会将其添加到 .h 文件中的 @interface 行,但除非其他类需要知道您的 View Controller 符合委托(delegate)协议(protocol),否则最好将其添加到.m 文件中的类扩展。

还有下面的代码:

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

应该只是:

imagePicker.delegate = self;

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

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

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

关于ios - 尝试实现相机功能时,xcode 将 "DetailViewController *"抛给不兼容类型警告的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15737600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com