My question: How can i let the user add a uiimageview by clicking a "+"-button. If the user clicks this button again, a second uiimageview should appear and so on...
My problem is, when I created multiple imageviews with multiple clicks on the "+"-button, i just can move the latest one, I created... how can I solve this problem?
Is imageViewGlobal an instance variable? You know that you are leaking object there, right? You never release previous ones. And if you are creating more of them, you can't just use a single one, you need an array. When a touch is detected you need to identify which image is touched and then move that image. There's a sample code project in the documentation called Touches which pretty much does just that.
I know how to detect which image is moving, but the problem is when I create the objects with the interface builder, I declare them in the header file and know the names of the imageViews, but when the user created the imageViews by clicking a button, the objects aren't declared in the header file. How can I differentiate the objects, if they don't have a name?
Yes, but how can I use arrays for that? In the Touches example code, they didn't use a function to add or delete objects... I only know how to use arrays for a tableview.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
// if the gesture recognizers's view isn't one of our pieces, don't allow simultaneous recognition
if (gestureRecognizer.view != ???)
// if the gesture recognizers are on different views, don't allow simultaneous recognition
if (gestureRecognizer.view != otherGestureRecognizer.view)
return NO;
// if either of the gesture recognizers is the long press, don't allow simultaneous recognition
if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] || [otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
return NO;
return YES;
}
What do I have to insert for the ??? if I don't have a name for the image?
You aren't, and shouldn't be, working with names. You work with pointers to objects. Each element the array will be a pointer to a uiimageview object, so will the gesutre recognizer's view property, so compare those by looping through the array.
Also, you're still leaking the newly created imageviews.
you are creating a new array each time, and most likely leaking the previous one at the same time. You should be creating it once, probably on viewDidLoad, and just adding to it from then on.
In the addObject: method in the for loop you never release the imageviews hence leaking them. You also never use 'image' from the first line of the for block.
I am using ARC.. It thought that was a new thing of Xcode 4.2 that should make programming more simple. I think I'm wrong, right?
They say it is, but I'm not sure yet
That code now looks better, but you're still creating a new array every time the gesture recognizer triggers. You should probably do it in initWithNibName (or Coder, whichever init you use) and be done with it.
Also, there seems to be no point to the for loop in that IBAction method since you hardcoded the start value to 0 and and the condition to <1, it will always run just once so you might as well remove it.
I did that, because I still was not really sure about what to insert for imageViewGlobal
Quote:
Originally Posted by baja_yu
You should probably do it in initWithNibName (or Coder, whichever init you use) and be done with it.
Like that?
Code:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
// Custom initialization of my two tab items on the tabbar controller
MyView *mView = [[MyView alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:mView, nil];
arryData = array;
return self;
}
Quote:
Originally Posted by baja_yu
Also, there seems to be no point to the for loop in that IBAction method since you hardcoded the start value to 0 and and the condition to <1, it will always run just once so you might as well remove it.
Sorry, but I'm pretty confused now... I'm not that new to Objective C programming, but I just don't it at the moment.. I feel stupid.