Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-10-2011, 05:05 PM   #1 (permalink)
Registered Member
 
vincefried's Avatar
 
Join Date: Apr 2010
Location: Hamburg, Germany
Posts: 38
vincefried is on a distinguished road
Default How to let the user add objects

Hello iPhoneDevSDK-Community!

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...

Thanks!
vincefried
__________________
My Apps:
Adalbert sagt.. (Germany only)
vincefried is offline   Reply With Quote
Old 10-10-2011, 05:23 PM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Alloc/init a new imageview just like any other object, set the image, then add it as a subview of the view where you want to show it.
baja_yu is offline   Reply With Quote
Old 10-11-2011, 10:14 AM   #3 (permalink)
Registered Member
 
vincefried's Avatar
 
Join Date: Apr 2010
Location: Hamburg, Germany
Posts: 38
vincefried is on a distinguished road
Default Thanks!

Thanks! That's what I did now:

Code:
-(IBAction) addObject:(id)sender {
    
    UIImageView *newImageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"Stuhl.png"]];
    newImageView.userInteractionEnabled = TRUE;
    [theView addSubview:newImageView];

    imageViewGlobal = newImageView;
    
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

	UITouch *touch = [[event allTouches] anyObject];
	CGPoint touchLocation = [touch locationInView:self.view];
	
	if ([touch view] == imageViewGlobal) {
        
        imageViewGlobal.center = touchLocation;
        
    }
}
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?

Thanks!
__________________
My Apps:
Adalbert sagt.. (Germany only)
vincefried is offline   Reply With Quote
Old 10-11-2011, 10:29 AM   #4 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

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.
baja_yu is offline   Reply With Quote
Old 10-11-2011, 12:28 PM   #5 (permalink)
Registered Member
 
vincefried's Avatar
 
Join Date: Apr 2010
Location: Hamburg, Germany
Posts: 38
vincefried is on a distinguished road
Default

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?
__________________
My Apps:
Adalbert sagt.. (Germany only)
vincefried is offline   Reply With Quote
Old 10-11-2011, 12:56 PM   #6 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Arrays...
baja_yu is offline   Reply With Quote
Old 10-11-2011, 02:12 PM   #7 (permalink)
Registered Member
 
vincefried's Avatar
 
Join Date: Apr 2010
Location: Hamburg, Germany
Posts: 38
vincefried is on a distinguished road
Default

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.
__________________
My Apps:
Adalbert sagt.. (Germany only)
vincefried is offline   Reply With Quote
Old 10-11-2011, 02:15 PM   #8 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

They didn't because they only have a 3 imageviews and that's it. How to use it? When you create a new view add it to the array as well.
baja_yu is offline   Reply With Quote
Old 10-11-2011, 02:19 PM   #9 (permalink)
Registered Member
 
vincefried's Avatar
 
Join Date: Apr 2010
Location: Hamburg, Germany
Posts: 38
vincefried is on a distinguished road
Default

Like that?

Code:
-(IBAction) addObject:(id)sender {
    
    UIImage * image = [UIImage imageNamed:@"Stuhl.png"];
    CGRect rect = CGRectMake(0.0f, 40.0f, image.size.width, image.size.height);
    
    UIImageView * newImageView = [[UIImageView alloc] initWithFrame:rect];
    [newImageView setImage:image];
    [theView addSubview:newImageView];
    newImageView.userInteractionEnabled = TRUE;

    [myArray addObject:newImageView];  
    
}
But now I have this method

Code:
- (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?
__________________
My Apps:
Adalbert sagt.. (Germany only)

Last edited by vincefried; 10-11-2011 at 02:21 PM.
vincefried is offline   Reply With Quote
Old 10-11-2011, 04:30 PM   #10 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

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.
baja_yu is offline   Reply With Quote
Old 11-13-2011, 08:35 AM   #11 (permalink)
Registered Member
 
vincefried's Avatar
 
Join Date: Apr 2010
Location: Hamburg, Germany
Posts: 38
vincefried is on a distinguished road
Default

Here's what I did so far:

Code:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{       
    arryData = [[NSMutableArray alloc] init]; 
    NSString *str = [NSString stringWithFormat:@"%@", [arryData count]];
    UIImage *image = [UIImage imageNamed:str];
    [imageViewGlobal setImage:image];
    
    if (gestureRecognizer.view != imageView && gestureRecognizer.view != imageView2 && gestureRecognizer.view != imageViewGlobal)
        return NO;
    
    if (gestureRecognizer.view != otherGestureRecognizer.view)
        return NO;
    
    if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] || [otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
        return NO;

    return YES;
}


-(IBAction) addObject:(id)sender {
   
    arryData = [[NSMutableArray alloc] init];
    int i;
    for (i = 0; i < 1; i++) {
        
        UIImage * image = [UIImage imageNamed:@"Stuhl.png"];
        CGRect rect = CGRectMake(0.0f, 40.0f, image.size.width, image.size.height);
        UIImageView * newImageView = [[UIImageView alloc] initWithFrame:rect];
        [newImageView setImage:[UIImage imageNamed:@"Stuhl.png"]];
        //newImageView.userInteractionEnabled = TRUE;
        [theView addSubview:newImageView];
        [arryData addObject:newImageView];

    }
    
}
It still doesn't work...
__________________
My Apps:
Adalbert sagt.. (Germany only)
vincefried is offline   Reply With Quote
Old 11-13-2011, 09:09 AM   #12 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Where and how is arrayData declared?

By doing this

Code:
arryData = [[NSMutableArray alloc] init];
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.
baja_yu is offline   Reply With Quote
Old 11-13-2011, 04:53 PM   #13 (permalink)
Registered Member
 
vincefried's Avatar
 
Join Date: Apr 2010
Location: Hamburg, Germany
Posts: 38
vincefried is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
Where and how is arrayData declared?
In the header file

Code:
 NSMutableArray *arryData;
Quote:
Originally Posted by baja_yu View Post
In the addObject: method in the for loop you never release the imageviews hence leaking them.
Yes, I know. That's because of Xcode 4.2. The compiler, it uses forbids releasing objects. Everytime I try to do it, I get an errormessage.

Quote:
Originally Posted by baja_yu View Post
You also never use 'image' from the first line of the for block.
I do, but it's not that necessary.

Code:
 image.size.height
__________________
My Apps:
Adalbert sagt.. (Germany only)
vincefried is offline   Reply With Quote
Old 11-13-2011, 05:52 PM   #14 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Quote:
Originally Posted by vincefried View Post
In the header file

Code:
 NSMutableArray *arryData;
Then you're leaking arrays with every subsequent call, and if you don't release in dealloc you're leaking the first one as well.

Quote:
Originally Posted by vincefried View Post
Yes, I know. That's because of Xcode 4.2. The compiler, it uses forbids releasing objects. Everytime I try to do it, I get an errormessage.
Please tell me that you're not actually blaming the compiler for your lack of memory management knowledge, or are you using ARC. What's the error?

Quote:
Originally Posted by vincefried View Post
I do, but it's not that necessary.

Code:
 image.size.height
In that case you should use 'image' in the 'newImageView setImage:' call instead of calling imageNamed again.
baja_yu is offline   Reply With Quote
Old 11-14-2011, 01:43 AM   #15 (permalink)
Registered Member
 
vincefried's Avatar
 
Join Date: Apr 2010
Location: Hamburg, Germany
Posts: 38
vincefried is on a distinguished road
Default

First, thanks for your engagement!

Quote:
Originally Posted by baja_yu View Post
Please tell me that you're not actually blaming the compiler for your lack of memory management knowledge, or are you using ARC. What's the error?
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?

Error:"ARC forbids explicit message send of 'release'"

Quote:
Originally Posted by baja_yu View Post
In that case you should use 'image' in the 'newImageView setImage:' call instead of calling imageNamed again.
Code:
-(IBAction) addObject:(id)sender {

***int i;
***for (i = 0; i < 1; i++) {

*******UIImage * image = [UIImage imageNamed:@"Stuhl.png"];
*******CGRect rect = CGRectMake(0.0f, 40.0f, image.size.width, image.size.height);
*******UIImageView * newImageView = [[UIImageView alloc] initWithFrame:rect];
*******[newImageView setImage:image];
*******newImageView.userInteractionEnabled = TRUE;
*******[theView addSubview:newImageView];
*******[arryData addObject:newImageView];

***}

}


Sure.. thanks.
__________________
My Apps:
Adalbert sagt.. (Germany only)
vincefried is offline   Reply With Quote
Old 11-14-2011, 01:45 AM   #16 (permalink)
Registered Member
 
vincefried's Avatar
 
Join Date: Apr 2010
Location: Hamburg, Germany
Posts: 38
vincefried is on a distinguished road
Default

Don't worry about the **** that's because I wrote
the reply with my iPhone.
__________________
My Apps:
Adalbert sagt.. (Germany only)
vincefried is offline   Reply With Quote
Old 11-14-2011, 08:03 AM   #17 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Quote:
Originally Posted by vincefried View Post
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.
baja_yu is offline   Reply With Quote
Old 11-14-2011, 10:39 AM   #18 (permalink)
Registered Member
 
vincefried's Avatar
 
Join Date: Apr 2010
Location: Hamburg, Germany
Posts: 38
vincefried is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
you're still creating a new array every time the gesture recognizer triggers.
You mean this?

Code:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{       
    NSString *str = [NSString stringWithFormat:@"%@", [arryData count]];
    UIImage *image = [UIImage imageNamed:str];
    [imageViewGlobal setImage:image];
    
    if (gestureRecognizer.view != imageView && gestureRecognizer.view != imageView2 && gestureRecognizer.view != imageViewGlobal)
        return NO;
    
    if (gestureRecognizer.view != otherGestureRecognizer.view)
        return NO;
    
    if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] || [otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
        return NO;

    return YES;
}
I did that, because I still was not really sure about what to insert for imageViewGlobal

Quote:
Originally Posted by baja_yu View Post
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 View Post
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.
__________________
My Apps:
Adalbert sagt.. (Germany only)
vincefried is offline   Reply With Quote
Old 11-14-2011, 12:08 PM   #19 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Quote:
Originally Posted by vincefried View Post
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.
This:

Code:
-(IBAction) addObject:(id)sender {
   //int i;
   //for (i = 0; i < 1; i++) {
      UIImage * image = [UIImage imageNamed:@"Stuhl.png"];
      CGRect rect = CGRectMake(0.0f, 40.0f, image.size.width, image.size.height);
      UIImageView * newImageView = [[UIImageView alloc] initWithFrame:rect];
      [newImageView setImage:image];
      newImageView.userInteractionEnabled = TRUE;
      [theView addSubview:newImageView];
      [arryData addObject:newImageView];
   //}
}
baja_yu is offline   Reply With Quote
Old 11-14-2011, 12:35 PM   #20 (permalink)
Registered Member
 
vincefried's Avatar
 
Join Date: Apr 2010
Location: Hamburg, Germany
Posts: 38
vincefried is on a distinguished road
Default

When I press on my add-button, a new imageview appears, but I'm still not able to move it around...
__________________
My Apps:
Adalbert sagt.. (Germany only)
vincefried is offline   Reply With Quote
Old 01-24-2012, 08:24 AM   #21 (permalink)
Registered Member
 
vincefried's Avatar
 
Join Date: Apr 2010
Location: Hamburg, Germany
Posts: 38
vincefried is on a distinguished road
Default Solved by myself

Never mind, I solved it by myself, but thanks for the answer. I just searched for the wrong method.

Code:
-(IBAction) addObject:(id)sender {

    UIImage * image = [UIImage imageNamed:@"Stuhl.png"];
    CGRect rect = CGRectMake(0.0f, 40.0f, image.size.width, image.size.height);
    Bild *newImageView = [[Bild alloc] initWithFrame:rect];
    [newImageView setImage:image];
    newImageView.userInteractionEnabled = TRUE;
    [theView addSubview:newImageView];
    /*--->*/[self addGestureRecognizersToPiece:newImageView];/*<---*/

}
__________________
My Apps:
Adalbert sagt.. (Germany only)
vincefried is offline   Reply With Quote
Reply

Bookmarks

Tags
add, create, generate, uiimageview, user

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 398
19 members and 379 guests
13dario13, 7twenty7, buggen, eski, EvilElf, glenn_sayers, HemiMG, jarv, LunarMoon, morterbaher, n00b, pbart, Pudding, QuantumDoja, sacha1996, Sami Gh, UMAD, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,673
Threads: 94,122
Posts: 402,906
Top Poster: BrianSlick (7,990)
Welcome to our newest member, morterbaher
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:22 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0