lamfan Posted August 5, 2008 Share Posted August 5, 2008 I'm using for loop creating multi UIViewImage in random location, so I need to dynamic create UIViewImage, how to combine the text + for number to create a new variable? for(int i = 1; i < 2; i++) { UIImageView *imageView = [..... imageView.image = [...... imageView.userInteractionEnabled = YES; self.view+i = imageView; <------------- How to combine view + i [imageView release]; [self addSubview:view+i]; <------------- How to combine view + i } and one more question, I had declare UIImageView view1 in the .h file do I need to declare all the UIImageView view1 UIImageView view2 in the .h file? Link to comment https://www.insanelymac.com/forum/topic/119395-how-to-combine-a-variable-and-text-together/ Share on other sites More sharing options...
alloutmacstoday Posted August 5, 2008 Share Posted August 5, 2008 I'm afraid you're out of luck Well, not completely... You could create an array, or a dictionary with all of the views as objects. Then, you get the object at index i. You will need to declare all of the views that you will use in the .h file. Or, you could only define the array in the .h file, and have the objects in that array, and when you need them, get them Link to comment https://www.insanelymac.com/forum/topic/119395-how-to-combine-a-variable-and-text-together/#findComment-845368 Share on other sites More sharing options...
stroke Posted August 5, 2008 Share Posted August 5, 2008 Whew, man, that code is screwed up. self.view returns a UIView, not an integer, so you can't add an integer to it. I assume you want to be able to add multiple image views, but still be able to keep track of them (remove them, whatever). In your header (.h) file, it should look something like this: @interface ClassName : ClassSuper { // any IBOutlets would be here } UIImageView *imageView; NSMutableArray *imageViewArray; -(void)addImageViews; @end Under your implementation (.m) file, you would do this. -(void) awakeFromNib { imageViewArray = [[NSMutableArray alloc] init]; } -(void) addImageViews { int i; for(i = 0; i < 2; i++) { imageView = [[UIImageView alloc] initWithFrame: …]; imageView.image = …; imageView.userInteractionEnabled = YES; [[self view] addSubview:imageView]; [imageViewArray addObject:imageView]; // this will keep the pointer to imageView intact for use later [imageView release]; } } Then, if you wanted to say, remove all the views, you would use this: -(void) removeAllImageViews { for(UIView *view in imageViewArray) { [view removeFromSuperview]; } [imageViewArray removeAllObjects]; } Link to comment https://www.insanelymac.com/forum/topic/119395-how-to-combine-a-variable-and-text-together/#findComment-845381 Share on other sites More sharing options...
lamfan Posted August 5, 2008 Author Share Posted August 5, 2008 Here is the code, the createTarget method can't be reuse...... I know that's not a good oo concept, anyone know how to change it? I don't want to duplicate same function again and again! help! MainView.h #import <UIKit/UIKit.h> @interface MainView : UIView { UIImageView *view1; UIImageView *view2; } @property (nonatomic, retain)UIImageView *view1; @property (nonatomic, retain)UIImageView *view2; - (void)createTarget1; - (void)createTarget2; @end MainView.m #import "MainView.h" @implementation MainView @synthesize view1, view2; - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self createTarget1]; [self createTarget2]; } return self; } - (void)createTarget1 { float number1 = random()%320; float number2 = random()%480; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(number1, number2, 57.0, 57.0)]; imageView.image = [UIImage imageNamed:@"Icon8.png"]; imageView.userInteractionEnabled = YES; self.view1 = imageView; [imageView release]; [self addSubview:view1]; } - (void)createTarget2 { float number1 = random()%320; float number2 = random()%480; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(number1, number2, 57.0, 57.0)]; imageView.image = [UIImage imageNamed:@"Icon8.png"]; imageView.userInteractionEnabled = YES; self.view2 = imageView; [imageView release]; [self addSubview:view2]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; if([touch view] == view1) { [view1 removeFromSuperview]; [self createTarget1]; }else if ([touch view] == view2) { [view2 removeFromSuperview]; [self createTarget2]; } } - (void)dealloc { [super dealloc]; } @end Link to comment https://www.insanelymac.com/forum/topic/119395-how-to-combine-a-variable-and-text-together/#findComment-845723 Share on other sites More sharing options...
alloutmacstoday Posted August 5, 2008 Share Posted August 5, 2008 Stroke: That is literally exactly what I told him to do... Well, you could use the array idea. Just make a function that adds an imageview to the screen and then to the mutable array. First you would have to give each imageview a tag that corresponds to where it is in the array. Then, in the touchesbegan method, you just remove the object at index (tag of the view that was clicked). Then, it will call the function again with an argement for what index to add the object to, and add a new imageview to the superview, and the mutable array Link to comment https://www.insanelymac.com/forum/topic/119395-how-to-combine-a-variable-and-text-together/#findComment-845906 Share on other sites More sharing options...
stroke Posted August 5, 2008 Share Posted August 5, 2008 I know, I was just being more verbose about it. @OP: why can't you reuse the method? Link to comment https://www.insanelymac.com/forum/topic/119395-how-to-combine-a-variable-and-text-together/#findComment-846251 Share on other sites More sharing options...
alloutmacstoday Posted August 5, 2008 Share Posted August 5, 2008 I know, I was just being more verbose about it. @OP: why can't you reuse the method? I think he was talking about his orginal method, that only worked for a certain view Link to comment https://www.insanelymac.com/forum/topic/119395-how-to-combine-a-variable-and-text-together/#findComment-846349 Share on other sites More sharing options...
lamfan Posted August 7, 2008 Author Share Posted August 7, 2008 Thanks the problem had solve! Link to comment https://www.insanelymac.com/forum/topic/119395-how-to-combine-a-variable-and-text-together/#findComment-848041 Share on other sites More sharing options...
Recommended Posts