gpt4 book ai didi

objective-c - iOS:简单的 NSMutableArray 调用

转载 作者:行者123 更新时间:2023-11-28 20:37:11 26 4
gpt4 key购买 nike

我需要在我的一种方法中调用 removeObject,但我不知道如何正确执行此操作。我是 Objective-C 的新手,仍在学习基础知识。我有一个应用程序,其行为有点像照片库,并显示 UIImageViews。我正在实现让用户从他们的图库中删除照片的选项。为此,我决定在每张图片上放置一个不可见的按钮。当用户点击“编辑”按钮时,每张图片上的隐藏删除按钮将变为事件状态(为简单起见,我在每个隐藏按钮上使用相同的 IBOutlet)。当用户点击图片上的按钮时,会出现一个警告 View ,询问他们是否真的要删除它。如果他们点击是,deleteAlertView 开始发挥作用:

- (void)deleteAlertView:(UIAlertView *)deleteButtonPressed
didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [deleteButtonPressed cancelButtonIndex]) {
[array removeObject:@"%@", deleteButtonPressed];
}

这里的问题是 [array removeObject:@"%@", deleteButtonPressed];,我做了 %@ 以便这将自动确定数组中的哪个对象被点击,而不是手动为每个 UIImageView 添加一个新的方法和按钮(我可能不得不这样做)。我收到有关“array”和“deleteButtonPressed”(使用未声明的标识符)的错误,我终究无法弄清楚应该放什么。我仍在学习基础知识以及这种语言的继承方式。任何帮助或建议都会很棒!我可能应该发布整个 View Controller 文件以显示相关的继承:

 - (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;
[picker dismissModalViewControllerAnimated:YES];
[self.popover dismissPopoverAnimated:YES];
return;

}

if (imageView2.image == nil) {
imageView2.image = img;
[picker dismissModalViewControllerAnimated:YES];
[self.popover dismissPopoverAnimated:YES];
return;
}
if (imageView3.image == nil) {
imageView3.image = img;
[picker dismissModalViewControllerAnimated:YES];
[self.popover dismissPopoverAnimated:YES];
return;
}
}

- (void)viewWillAppear:(BOOL)animated
{

self.user = [NSUserDefaults standardUserDefaults];

NSMutableArray* array = [[self.user objectForKey:@"images"]mutableCopy];
while(array == nil)
{
[self.user setObject:[NSMutableArray arrayWithObject:@""] forKey:@"images"];
array = [[self.user objectForKey:@"images"]mutableCopy];
NSLog(@"%@",@"attempting to create an array to store the images in");
}

}

- (void)applicationDidEnterBackground:(UIApplication*)application {
NSLog(@"Image on didenterbackground: %@", imageView);
NSMutableArray* array = [NSMutableArray arrayWithObject:[NSData dataWithData:UIImagePNGRepresentation(imageView.image)]];

[array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView2.image)]];
[array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView3.image)]];

[self.user setObject:array forKey:@"images"];
[user synchronize];

}

- (void)viewDidLoad
{
self.user = [NSUserDefaults standardUserDefaults];
NSLog(@"It is %@", self.user);
NSMutableArray* array = [[self.user objectForKey:@"images"]mutableCopy];
imageView.image = [[UIImage alloc] initWithData:[array objectAtIndex:0]];
imageView2.image = [[UIImage alloc] initWithData:[array objectAtIndex:1]];
imageView3.image = [[UIImage alloc] initWithData:[array objectAtIndex:2]];


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];

}

- (IBAction)deleteButtonPressed:(id)sender {
UIAlertView *deleteAlertView = [[UIAlertView alloc] initWithTitle:@"Delete"
message:@"Are you sure you want to delete this photo?"
delegate:self
cancelButtonTitle:@"Yes"
otherButtonTitles:@"No", nil];
[deleteAlertView show];

}

- (void)deleteAlertView:(UIAlertView *)deleteButtonPressed
didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [deleteButtonPressed cancelButtonIndex]) {
[array removeObject:@"%@", deleteButtonPressed];
}

}

最佳答案

这里有一个问题,但首先是一个相对次要的问题:

 - (void)deleteAlertView:(UIAlertView *)deleteButtonPressed didDismissWithButtonIndex:(NSInteger)buttonIndex

短语“deleteButtonPressed”暗示偶数,因为它以动词结尾。它实际上指的是一个对象,具体指的是一个UIAlertView类型的参数。您应该将其命名为更像是 AlertView 的名称。

其次,这一行是完全错误的:

[array removeObject:@"%@", deleteButtonPressed];

您正试图删除一个字符串。如果该方法接受一个参数列表(您在其中传递以逗号分隔的多个对象),您将删除字面上的“deleteButtonPressed”。您想要删除 deleteButtonPressed 变量指向的对象。所以你所要做的就是:

 [array removeObject:deleteButtonPressed];

关于objective-c - iOS:简单的 NSMutableArray 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9985700/

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