-1

我正在创建一个存储UIImage在其中的应用程序NSMutableArray

    -(void)mainImageSelected: (id)sender
{    
    mainImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"image%d.png",[sender tag]]]];
    [myArray addObject:mainImageView];
}

现在,我想在用户触摸的地方显示这个图像。我正在做这样的事情:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];

    if ([currentObject isEqualToString:@"myObject"])
    {
            NSLog(@"Hello World");
            [self.view addSubview:[myArray lastObject]];
            [[myArray lastObject]setCenter:point];
    }
}

到这里为止,它工作正常,但问题是我想在用户按下撤消按钮时删除此图像。为此,我正在做这样的事情:

-(IBAction)Undo
{
    [myArray removeLastObject];
}

它正在从数组中删除对象,但图像没有被删除/删除。任何人请解决这个问题。

4

3 回答 3

8
-(IBAction)decorationUndo
{
    [decorationArray removeLastObject];
    [self.view.subviews removeLastObject];
    [self setNeedsDisplay];
}
于 2012-12-26T11:15:50.783 回答
3

您还应该在将图像从数组中删除之前从它所在的视图中删除它:

 -(IBAction)decorationUndo
{
  [[myArray lastObject] removeFromSuperview];
  [myArray removeLastObject];
}

在上面的代码中,我假设这是一个包含您的图像decorationArray的对象数组。UIImageView

于 2012-12-26T11:16:17.987 回答
2

在从数组中删除它之前,首先将它从视图中删除。

-(IBAction)Undo
{
    [[myArray lastObject] removeFromSuperview];
    [myArray removeLastObject];
}
于 2012-12-26T11:19:43.377 回答