开发者

creating an app with several UIImageViews

开发者 https://www.devze.com 2023-01-27 01:45 出处:网络
I am creating an app that will require several UIImageViews, ranging anywhere from one to two hundred images, each image can be moved around by the user. To do this I can declare all the images in the

I am creating an app that will require several UIImageViews, ranging anywhere from one to two hundred images, each image can be moved around by the user. To do this I can declare all the images in the header, and write all the code separately for each image, but this will take too long and leave a lot of room for error

is it possible to do somethin开发者_如何学Cg like the following?

(every time the button is touched it creates another image, but each creates a new name..... image1, image2, image3......)

-(IBAction)button{ 

    int X;

    X++;

    UIImageView *imageX = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"name.png"]];

    [self.view addSubview:hatHighlighted1];
}


If you don't need to reference these, you don't really even need to keep a counter variable. Go ahead and create a new one on every touch.

If you do need to identify each one, keep a counter variable, and set each UIImageView's tag on creation. In your header,

@interface YourViewController : UIViewController {
    int _counter;
}
...

and in your implementation,

@implementation YourViewController

//use this, or your preferred initializer
- (void)viewDidLoad {
  _counter = 0; //to initialize
}

-(IBAction)buttonPressed:(id)sender {

    UIImageView *newImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"name.png"]];
    [newImage setTag:_counter];
    _counter++; 

    [self.view addSubview:newImage];
    [newImage release];
}    
...
0

精彩评论

暂无评论...
验证码 换一张
取 消