문제

내 생각은 생성된 정수 값을 UIImageView 이름에 추가하는 것입니다.예는 다음과 같습니다.imageView1, imageView2, imageView3이라는 세 개의 UIImageView가 있습니다.버튼을 누르면 다음과 같은 간단한 코드가 표시됩니다.self.value = value + 1;(물론 이 "값"이 선언되어 있습니다.) 이제 다음과 같은 코드를 추가하고 싶습니다.UIImage *img = [UIImage imageNamed:@"image.png"]; [imageView(value) setImage:img];그래서 저는 이 코드 부분을 원합니다 ...[imageView(value)... 위에서 정의한 값을 사용합니다.어떻게 해야 합니까?

도움이 되었습니까?

해결책

당신은 설정할 수 있습니다 tag 에 대한 귀하의 이미지 보기

// set tag
[imageView1 setTag:1];
[imageView2 setTag:2];
//...

// get the image view with the tag
[(UIImageView *)[self.view viewWithTag:value] setImage:img];
//...

다른 팁

당신이 사용할 수 있는 태그,또는 당신을 넣을 수 있습니다 UIImageViewsNSArray.다음을 할 수 있는 뭔가

[((UIImageView *)[imageViewArray objectAtIndex:self.value]) setImage:img];

여기에 나가려고 했습 귀하의 요구 사항에 따라.

내 ViewController 클래스

- (void)viewDidLoad
{   
[super viewDidLoad];
ExtendedUIImageView *eimage=[[ExtendedUIImageView alloc]initExtendedUIImageViewWithFrame:CGRectMake(0, 0, 768, 1024)];
UIImage *img1=[UIImage imageNamed:@"mac.png"];
UIImage *img2=[UIImage imageNamed:@"images.jpeg"];
UIImage *img3=[UIImage imageNamed:@"mac.png"];
[eimage addUIImage:img1 ToExtendedImageViewWithRect:CGRectMake(100, 100, 100, 100) withTag:1];
[eimage addUIImage:img2 ToExtendedImageViewWithRect:CGRectMake(210, 100, 100, 100) withTag:2];
[eimage addUIImage:img3 ToExtendedImageViewWithRect:CGRectMake(320, 100, 100, 100) withTag:3];
[self.view addSubview:eimage];


}

- (IBAction)chageImage:(UIButton *)sender {
    NSLog(@"#####");
     UIImage *img2=[UIImage imageNamed:@"images.jpeg"];
    [eimage replaceImage:img2 forTag:3];
}

나는 확장 UIView 클래스고 만들어진 새로운 클래스 ExtendedImageView 클래스

ExtendedImageView.h

@interface ExtendedUIImageView : UIView
-(id)initExtendedUIImageViewWithFrame:(CGRect)rect;
-(void)addUIImage:(UIImage *)img ToExtendedImageViewWithRect:(CGRect)rect withTag:(int)n;
-(void)replaceImage:(UIImage *)newImg forTag:(int)n;    
@end

ExtendedImageView.m

@implementation ExtendedUIImageView
-(id)initExtendedUIImageViewWithFrame:(CGRect)rect{
    if([super initWithFrame:rect]){
        return  self;
    }
    return nil;
}

-(void)addUIImage:(UIImage *)img ToExtendedImageViewWithRect:(CGRect)rect withTag:(int)n{
    UIImageView *imgView=[[UIImageView alloc]initWithImage:img];
    //[imgView setBackgroundColor:[UIColor grayColor]];
    imgView.frame=rect;
    [imgView setTag:n];
    [self addSubview:imgView];

}

-(void)replaceImage:(UIImage *)newImg forTag:(int)n{
    for(UIView * img in [self subviews]){
        if([img isKindOfClass:[UIImageView class]]&&[img tag]==n){
            ((UIImageView *)img).image=newImg;
        }
    }
}
@end
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top