刷新视图或删除子视图ios

时间:2012-08-20 18:01:48

标签: iphone objective-c ios

我正在尝试制作一个用户可以在屏幕上滑动以在帖子之间浏览的场景。帖子既可以是带有文字的图像,也可以只是一个音符,视图会根据发生的情况而改变。

获取过程非常完美。无论我向右还是向左滑动,它都会得到正确的帖子。问题是旧视图不会消失,视图重叠。当你从一张纸条转到一张照片时,这尤其令人烦恼,反之亦然,因为尺寸不同......

以下是代码:

- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@" GETRESULT %@   POSTNUMBER %d", getResult, postNumber);
Post *postInfo = [[Post alloc] init];

postInfo = [getResult objectAtIndex:postNumber];

UITextView *postText = [[UITextView alloc] init];
imgView = [[UIImageView alloc] init];

NSString *getImageString = postInfo.attachments;
if(getImageString){
    postText = [[UITextView alloc] init];
    postText.text = postInfo.noteText;
    [postText setFrame:CGRectMake(20, 320, 280, 80)];
    NSLog(@"IMG1");
}else {
    postText = [[UITextView alloc] init];
    postText.text = postInfo.noteText;
    [postText setFrame:CGRectMake(20, 10, 280, 240)];
    NSLog(@"TEXT1");
}
[self.view addSubview:postText];
SHOWHUD(self.view);
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{

        NSURL *url = [NSURL URLWithString:getImageString];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *img = [[UIImage alloc] initWithData:data];
        [imgView setFrame:CGRectMake(20, 10, 280, 300)];
        [imgView setContentMode:UIViewContentModeScaleAspectFit];
        [imgView setImage:img];

    dispatch_async(dispatch_get_main_queue(), ^{   
        [self.view addSubview:imgView];
        HIDEHUD(self.view);
    });
});


UISwipeGestureRecognizer *recognizerRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
[[self view] addGestureRecognizer:recognizerRight];

UISwipeGestureRecognizer *recognizerLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft:)];
[recognizerLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:recognizerLeft];

// Do any additional setup after loading the view.
}

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer{
NSLog(@"SWIPE RIGHT");
if(postNumber > 0){
    postNumber--;
    Post *postInfo = [[Post alloc] init];

    postInfo = [getResult objectAtIndex:postNumber];

    UITextView *postText = [[UITextView alloc] init];
    imgView = [[UIImageView alloc] init];

    NSString *getImageString = postInfo.attachments;
    if(getImageString){
        [postText removeFromSuperview];
        postText = [[UITextView alloc] init];
        postText.text = postInfo.noteText;
        [postText setFrame:CGRectMake(20, 320, 280, 80)];
        NSLog(@"IMG1");
    }else {
        [postText removeFromSuperview];
        postText = [[UITextView alloc] init];
        postText.text = postInfo.noteText;
        [postText setFrame:CGRectMake(20, 10, 280, 240)];
        NSLog(@"TEXT1");
    }
    [self.view addSubview:postText];
    SHOWHUD(self.view);
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        if (getImageString) {

        NSURL *url = [NSURL URLWithString:getImageString];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *img = [[UIImage alloc] initWithData:data];
        [imgView setFrame:CGRectMake(20, 10, 280, 300)];
        [imgView setContentMode:UIViewContentModeScaleAspectFit];
        [imgView setImage:img];
        }else {
            [imgView removeFromSuperview];
        }
        dispatch_async(dispatch_get_main_queue(), ^{   
            [self.view addSubview:imgView];
            HIDEHUD(self.view);
        });
    });
}

}

- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer{
NSLog(@"SWIPE LEFT");
if(postNumber < [getResult count] - 1){
    postNumber++;

    Post *postInfo = [[Post alloc] init];

    postInfo = [getResult objectAtIndex:postNumber];

    UITextView *postText = [[UITextView alloc] init];
    imgView = [[UIImageView alloc] init];

    NSString *getImageString = postInfo.attachments;
    if(getImageString){
        [postText removeFromSuperview];
        postText = [[UITextView alloc] init];
        postText.text = postInfo.noteText;
        [postText setFrame:CGRectMake(20, 320, 280, 80)];
        NSLog(@"IMG1");
    }else {
        [postText removeFromSuperview];
        postText = [[UITextView alloc] init];
        postText.text = postInfo.noteText;
        [postText setFrame:CGRectMake(20, 10, 280, 240)];
        NSLog(@"TEXT1");
    }
    [self.view addSubview:postText];
    SHOWHUD(self.view);
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        if(getImageString){
        NSURL *url = [NSURL URLWithString:getImageString];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *img = [[UIImage alloc] initWithData:data];
        [imgView setFrame:CGRectMake(20, 10, 280, 300)];
        [imgView setContentMode:UIViewContentModeScaleAspectFit];
        [imgView setImage:img];
        }else {
            [imgView removeFromSuperview];
        }
        dispatch_async(dispatch_get_main_queue(), ^{   
            [self.view addSubview:imgView];
            HIDEHUD(self.view);
        });
    });

}
}

起初我只增加了postNumber并在滑动操作中调用了viewDidLoad。这给了我完全相同的结果......这只是无数次尝试和错误尝试之一,我很抱歉这是一个更加混乱的... ...

如果有人得到解决方案,我将非常感激。

提前致谢, 汤姆

2 个答案:

答案 0 :(得分:0)

你试过这个吗?

for (UIView * view in self.view.subviews) {
    if ([view isEqual:postText]||[view isEqual:imgView]) {
        [view removeFromSuperview];
    }

}

答案 1 :(得分:0)

我无法从superview中删除变量,因为我是在viewDidLoad的新实例中完成的。我这次尝试在调用viewDidLoad之前尝试这样做,并且它有效!

抱歉浪费你的时间......

汤姆