Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 按钮在数组中删除,但不在视图中删除_Iphone_Ios_Xcode - Fatal编程技术网

Iphone 按钮在数组中删除,但不在视图中删除

Iphone 按钮在数组中删除,但不在视图中删除,iphone,ios,xcode,Iphone,Ios,Xcode,我在删除scrollView中的图像时遇到问题,我将图像放在UIScrollView中,然后单击“添加”按钮,当单击时将提示一个警报视图,询问是否要删除它。如果是,则删除我的NSDocumentDirectory,但不在我的视图中。我的图像来自NSDocumentDirectory从ImagePicker中拾取的图像 - (void)addImage:(UIImage *)image { [_images addObject:image]; [_thumbs addObject:

我在删除scrollView中的图像时遇到问题,我将图像放在UIScrollView中,然后单击“添加”按钮,当单击时将提示一个警报视图,询问是否要删除它。如果是,则删除我的
NSDocumentDirectory
,但不在我的视图中。我的图像来自
NSDocumentDirectory
ImagePicker
中拾取的图像

- (void)addImage:(UIImage *)image {
    [_images addObject:image];
    [_thumbs addObject:[image imageByScalingAndCroppingForSize:CGSizeMake(60, 60)]];
    [self createScrollView];
}

- (void) createScrollView {

    [scrollView setNeedsDisplay];
    int row = 0;
    int column = 0;
    for(int i = 0; i < _thumbs.count; ++i) {

        UIImage *thumb = [_thumbs objectAtIndex:i];
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(column*60+10, row*60+10, 60, 75);
        [button setImage:thumb forState:UIControlStateNormal];
        [button addTarget:self 
                   action:@selector(buttonClicked:) 
         forControlEvents:UIControlEventTouchUpInside];
        button.tag = i; 

        [scrollView addSubview:button];

        if (column == 4) {
            column = 0;
            row++;
        } else {
            column++;
        }

    }
   [scrollView setContentSize:CGSizeMake(300, (row+1) * 60 + 10)];
}

- (IBAction)buttonClicked:(id)sender {
        _clickedButton = (UIButton *)sender;
        UIAlertView *saveMessage = [[UIAlertView alloc] initWithTitle:@""
                                                              message:@"DELETE?"
                                                             delegate:self
                                                    cancelButtonTitle:@"NO"
                                                    otherButtonTitles:@"YES", nil];
        [saveMessage show];  
   }

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"YES"]) {
            UIButton *button = _clickedButton;
            [button removeFromSuperview];
            [_images objectAtIndex:button.tag];
            [_images removeObjectAtIndex:button.tag];

            NSFileManager *fileManager = [NSFileManager defaultManager];
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"images%lu.png", button.tag]];
            [fileManager removeItemAtPath: fullPath error:NULL];
            NSLog(@"image removed");
}

- (void)viewDidLoad
{
        self.slotBg = [[UIView alloc] initWithFrame:CGRectMake(43, 370, 310, 143)];
        CAGradientLayer *gradient = [CAGradientLayer layer];
        gradient.frame = self.slotBg.bounds;
        gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor grayColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
        [self.slotBg.layer insertSublayer:gradient atIndex:0];
        [self.view addSubview:self.slotBg];
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,300.0f,130.0f)];
        [slotBg addSubview:self.scrollView];
}

- (void)viewDidAppear:(BOOL)animated
{
    [_thumbs removeAllObjects];
    for(int i = 0; i <= 100; i++) 
    { 
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDir = [paths objectAtIndex:0];

        NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"oneSlotImages%d.png", i]]; 
        NSLog(@"savedImagePath=%@",savedImagePath);
        if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
            [self addImage:[UIImage imageWithContentsOfFile:savedImagePath]]; 
            NSLog(@"file exists");
        } 
    } 
    NSLog(@"Count : %d", [_images count]);
}
-(无效)添加图像:(UIImage*)图像{
[_imagesaddobject:image];
[_thumbsaddobject:[image-imagebyscalingandcropingforsize:CGSizeMake(60,60)];
[自创建滚动视图];
}
-(void)创建滚动视图{
[滚动视图设置需要显示];
int行=0;
int列=0;
对于(int i=0;i<_thumbs.count;++i){
UIImage*thumb=[\u thumbs objectAtIndex:i];
UIButton*button=[UIButton button类型:UIButtonTypeCustom];
button.frame=CGRectMake(列*60+10,行*60+10,60,75);
[按钮设置图像:thumb for状态:uicontrol状态正常];
[按钮添加目标:自我
操作:@选择器(按钮点击:)
forControlEvents:UIControlEventTouchUpInside];
button.tag=i;
[滚动视图添加子视图:按钮];
如果(列==4){
列=0;
行++;
}否则{
列++;
}
}
[scrollView setContentSize:CGSizeMake(300,(行+1)*60+10)];
}
-(iAction)按钮点击:(id)发送者{
_单击按钮=(UIButton*)发送者;
UIAlertView*saveMessage=[[UIAlertView alloc]initWithTitle:@]
消息:@“删除?”
代表:赛尔夫
取消按钮:@“否”
其他按钮提示:@“是”,无];
[保存消息显示];
}
-(无效)alertView:(UIAlertView*)alertView单击按钮索引:(NSInteger)按钮索引
{
NSString*标题=[alertView buttonTitleAtIndex:buttonIndex];
如果([标题isEqualToString:@“是”]){
ui按钮*按钮=_点击按钮;
[从SuperView中移除按钮];
[\u images objectAtIndex:button.tag];
[\u images removeObjectAtIndex:button.tag];
NSFileManager*fileManager=[NSFileManager defaultManager];
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*documentsDirectory=[paths objectAtIndex:0];
NSString*fullPath=[DocumentsDirectoryStringByAppendingPathComponent:[NSSTRINGStringWithFormat:@“images%lu.png”,button.tag]];
[fileManager removeItemAtPath:完整路径错误:NULL];
NSLog(@“图像已删除”);
}
-(无效)viewDidLoad
{
self.slotBg=[[UIView alloc]initWithFrame:CGRectMake(43370310143)];
CAGradientLayer*梯度=[CAGradientLayer层];
gradient.frame=self.slotBg.bounds;
gradient.colors=[NSArray arrayWithObjects:(id)[[UIColor grayColor]CGColor],(id)[[UIColor WHITECLOR]CGColor],nil];
[self.slotBg.layer insertSublayer:梯度指数:0];
[self.view addSubview:self.slotBg];
self.scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0.0f、0.0f、300.0f、130.0f)];
[slotBg addSubview:self.scrollView];
}
-(无效)视图显示:(BOOL)动画
{
[[拇指移除所有对象];

对于(inti=0;i将标记值设置为button。然后通过

    (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
         UIButton *btn = (UIButton*)[scrollview viewWithTag:button.tag];
         //your button tag
         [btn removeFromSuperView];
}


每次向数组中添加图像时,您都会调用
createScrollView
。它所做的似乎是创建包含彼此顶部相同图像的按钮。您的代码似乎会将其从视图中删除,但它的下方似乎有类似的按钮。因此,只有在您完成后才能调用
createScrollView
'已添加所有图像并将其从您的
添加图像中删除:

for(int i = 0; i <= 100; i++) 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];

    NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"oneSlotImages%d.png", i]]; 
    NSLog(@"savedImagePath=%@",savedImagePath);
    if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
        [self addImage:[UIImage imageWithContentsOfFile:savedImagePath]]; 
        NSLog(@"file exists");
    } 
}
[self createScrollView];
for(int i=0;i
希望这将帮助您……

尝试以下方法:

- (void)removeObjectWithTag:(int)objectTag
{
    [_thumbs removeObjectAtIndex:objectTag];

    for (UIView *view in scrollView.subviews)
    {
        [view removeFromSuperview];
    }

    scrollView.contentSize = CGSizeZero;
    [self createScrollView];
}

然后使用NSLog在创建和删除的两个位置打印按钮引用。当我添加另一个图像时,它不会更新scrollview中的图像。@Kobe.o4:如果您的意思是在调用If outside of ViewDidDisplay:,则必须编写代码以确保它将其添加到滚动视图中。否则,快速修复方法是调用[scrollview.subviews valueForKey:@“removeFromSuperview”]在createScrollView的开头。显然这不是最好的方法,但会完成的。我正在做的是去掉所有的拇指,然后重新加载。@Kobe.o4但即使这样做,按钮仍会在scrollview中。你尝试实现我的答案了吗?@Kobe.o4你能告诉我当你不这样做时到底发生了什么吗ried上述代码+[scrollview.subviews valueForKey:@“removeFromSuperview”]?是,在调用它之前尝试运行以下代码..NSArray*arr=[scrollview子视图];for(UIView*view in arr)[view removeFromSuperview];然后调用[self-ViewDidDisplay:yes];
- (void)removeObjectWithTag:(int)objectTag
{
    [_thumbs removeObjectAtIndex:objectTag];

    for (UIView *view in scrollView.subviews)
    {
        [view removeFromSuperview];
    }

    scrollView.contentSize = CGSizeZero;
    [self createScrollView];
}