我正在撰写的这个应用程序部分遇到很多问题,我相信这里的人都会厌倦我,所以我会尝试在这篇文章中解决我的所有问题。我正在开发一个行为像照片库的应用程序,我正在实施让用户从其库中删除照片的选项。为了做到这一点,我决定在每张照片上放置一个隐形按钮。当用户点击“编辑”按钮时,每张图片上的隐藏删除按钮变为活动状态。为简单起见,我在每个隐藏按钮上使用相同的IBOutlet,并且我在Interface Builder中适当地标记了每个按钮。当用户点击图片上的按钮时,会出现警告视图,询问他们是否确实要删除它。如果他们单击是,我调用removeObjectAtIndex并从UI中删除图像。但是,当我在警报视图中单击“是”时,我从Xcode收到错误说明:
2012-04-04 11:26:40.484 AppName [608:f803] - [UIButton setImage:]:无法识别的选择器发送到实例0x6a922c0
2012-04-04 11:26:40.485 AppName [608:f803] 由于未捕获的异常'NSInvalidArgumentException'而终止应用,原因:' - [UIButton setImage:]:无法识别的选择器发送到实例0x6a922c0' *
我一直试图找出造成这种情况的原因几个小时现在无济于事。我没有在我的代码中的任何地方设置UIButton的图像。我在IB中做过,但我只是将按钮类型设置为Custom,使它们看起来不可见。我将在下面发布我的整个文件,我在代码中找不到任何问题,所以非常感谢任何帮助!谢谢。 编辑以下是代码的当前版本:
- (IBAction)grabImage {
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
_popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
[_popover presentPopoverFromRect:self.imageView.bounds inView:self.imageView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else {
[self presentModalViewController:imgPicker animated:YES];
}
[self.imgPicker resignFirstResponder];
}
// Sets the image in the UIImageView
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
if (imageView.image == nil) {
imageView.image = img;
self.array = [NSMutableArray arrayWithObject:[NSData dataWithData:UIImagePNGRepresentation(imageView.image)]];
[picker dismissModalViewControllerAnimated:YES];
[self.popover dismissPopoverAnimated:YES];
return;
}
if (imageView2.image == nil) {
imageView2.image = img;
NSLog(@"The image is a %@", imageView);
[self.array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView2.image)]];
[picker dismissModalViewControllerAnimated:YES];
[self.popover dismissPopoverAnimated:YES];
return;
}
if (imageView3.image == nil) {
imageView3.image = img;
[self.array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView3.image)]];
[picker dismissModalViewControllerAnimated:YES];
[self.popover dismissPopoverAnimated:YES];
return;
}
if (imageView4.image == nil) {
imageView4.image = img;
[self.array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView4.image)]];
[picker dismissModalViewControllerAnimated:YES];
[self.popover dismissPopoverAnimated:YES];
return;
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"Photo Gallery", @"Photo Gallery");
self.tabBarItem.image = [UIImage imageNamed:@"42-photos.png"];
}
return self;
}
////start of saving////
- (void)applicationDidEnterBackground:(UIApplication*)application {
NSLog(@"Image on didenterbackground: %@", imageView);
self.array = [NSMutableArray arrayWithObject:[NSData dataWithData:UIImagePNGRepresentation(imageView.image)]];
[self.array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView2.image)]];
[self.array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView3.image)]];
[self.array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView4.image)]];
[self.user setObject:self.array forKey:@"images"];
[user synchronize];
}
- (void)viewDidLoad
{
self.user = [NSUserDefaults standardUserDefaults];
NSLog(@"It is %@", self.user);
self.array = [[self.user objectForKey:@"images"]mutableCopy];
imageView.image = [[UIImage alloc] initWithData:[self.array objectAtIndex:0]];
imageView2.image = [[UIImage alloc] initWithData:[self.array objectAtIndex:1]];
imageView3.image = [[UIImage alloc] initWithData:[self.array objectAtIndex:2]];
imageView4.image = [[UIImage alloc] initWithData:[self.array objectAtIndex:3]];
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:app];
backToGalleryButton.hidden = YES;
tapToDeleteLabel.hidden = YES;
deleteButton1.hidden = YES;
[super viewDidLoad];
}
///// shows the hidden and invisible "delete" button over each photo.
- (IBAction)editButtonPressed:(id)sender {
grabButton.hidden = YES;
editButton.hidden = YES;
backToGalleryButton.hidden = NO;
tapToDeleteLabel.hidden = NO;
deleteButton1.hidden = NO;
}
////
// This is when the user taps on the image to delete it.
- (IBAction)deleteButtonPressed:(id)sender {
NSLog(@"Sender is %@", sender);
UIAlertView *deleteAlertView = [[UIAlertView alloc] initWithTitle:@"Delete"
message:@"Are you sure you want to delete this photo?"
delegate:self
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes", nil];
[deleteAlertView show];
int imageIndex = ((UIButton *)sender).tag;
deleteAlertView.tag = imageIndex;
}
- (void)alertView: (UIAlertView *) alertView
clickedButtonAtIndex: (NSInteger) buttonIndex
{
if (buttonIndex != [alertView cancelButtonIndex]) {
NSLog(@"User Clicked Yes. Deleting index %d of %d", alertView.tag, [array count]);
NSLog(@"The tag is %i", alertView.tag);
[self.array removeObjectAtIndex: alertView.tag];
NSLog(@"After deleting item, array count = %d", [array count]);
NSLog(@"Returned view is :%@, in view: %@", [self.view viewWithTag:alertView.tag], self.view);
((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil;
}
[self.user setObject:self.array forKey:@"images"];
}
更新:我添加了断点,并发现((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil;
是导致崩溃的行,我仍然无法弄清楚原因。
答案 0 :(得分:2)
我没有看到您粘贴到问题中的任何代码中都调用了setImage:
,但我可以告诉您:UIButton
的{{1}}方法实际上需要一个按钮将州作为第二个参数。
e.g。
[UIButton setImage: forState:]
我已经为您链接了Apple的文档。
如果您对setImage
的来电没有第二个参数,则可以解释您所看到的“无法识别的选择器错误”。
答案 1 :(得分:1)
这是@MichaelDautermann的扩展,他非常清楚UIButton有一个setImage:forState:方法,但没有setImage方法。但是,您正在调用set image。不是按钮,而是当你说imageView.image和imageView2.image。这将调用图像视图的setter方法。我会设置一个断点(或使用NSLog和%@项)来确保imageView实际上是一个imageView而不是一个按钮。如果它从某种程度上改变了你,这可能导致问题。只需在这两行设置一个断点,看看你是否通过它们。
此外,如果Xcode没有弹出到实际导致问题的那一行,请检查崩溃日志。符号化,日志将为您提供行号。或者,一种不太直接的方法是在答案中提供的方法的末尾设置断点,并查看您通过的数量。一旦崩溃,您可以缩小导致您悲伤的方法,然后开始在方法中设置断点,直到您到达相关的行。
的更新:强> 的 你在评论中说过((UIImageView *)[self.view viewWithTag:alertView.tag])。image = nil就是你所拥有的,但是self.view是一个UIControl。更改演员表不会影响结果。 UIImageView的图像是你试图删除UIControl的子视图吗?如果没有,你永远不会从viewWithTag返回图像视图。通常,self.view指的是一个视图控制器的视图,所以如果你得到一个UIControl,我的假设是你的整个视图是一个UIControl,或者你在错误的类中这样做。你在做哪个类,它的子类是什么?看来你在视图控制器中这样做了,但我只是想确定一下。而且,在任何一种情况下(UIControl或UIImageView),两个类都不响应setImage。