Calling a void function from an IBAction?

Discussion in 'Mac Programming' started by Darkroom, Nov 3, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    not sure how to do this properly, although i believe it's easy (or maybe it's more complex then i assume)...

    Code:
    - (id)imageBrowser:(IKImageBrowserView *) view itemAtIndex:(int) index
    	{
    	return [images objectAtIndex:index];
    	}
    
    - (void)imageBrowser:(IKImageBrowserView*)view removeItemsAtIndexes: (NSIndexSet*)indexes
    	{
    	[images removeObjectsAtIndexes:indexes];
    	}
    		
    - (IBAction)removeSwatchButton:(id)sender
    	{
    	NSLog (@"Remove Button");
    	[self imageBrowser:(IKImageBrowserView*)view removeItemsAtIndexes: (NSIndexSet*)indexes];
    	}
    
    so i'm trying to call the second method from the third action method... both are in the same class... compiler states that "view" and "indexes" are undeclared... i've included the first method to show that the first and second are similar, so i can't simply write [self imageBrowser:nil];, which is what i often do for calling other functions that don't have lots of instance variables and other call functions attached.
     
  2. Krevnik macrumors 68040

    Krevnik

    Joined:
    Sep 8, 2003
    #2
    Without knowing more, I'd say the compiler is right... view and indexes aren't declared when you pass them into the method from the IBAction.

    What view and what indexes are you trying to pass? Where do they live? Are they instance members?
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    Hm. Not sure what images is, and it doesn't seem like view is used at all in the first two functions. In the third function... the only local variable you have is the argument sender. How do you know what NSIndexSet you want to remove? You don't have any NSIndexSet declared in this function, so you have nothing to pass to the second function.

    Code:
    NSIndexSet *myIndexes = [NSIndexSet indexSetWithIndex:[sender intValue]];
    [self imageBrowser:nil removeItemsAtIndexes:myIndexes];
    would work.

    Code:
    [self imageBrowser:nil removeItemsAtIndexes:nil];
    should work also, but... doesn't make much sense.

    -Lee
     
  4. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #4
    thanks for the responses...

    lee, neither worked... i'm using apple's code from their ImageBrowser application, which is a simplistic look at the IKImageBrowser class... the problem is that i'm not so strong with understanding what exactly is going on behind the scenes...

    at first i tried cheating by simply adding a button with a delete key equivalent, as when the user selects items in the image browser, they are removed by pressing delete... unfortunately for me it didn't work...

    i've attached the class files hoping you or someone doesn't mind taking a quick look...my IBActions are at the very end of the the implementation file...
     

    Attached Files:

  5. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #5
    the index is indexing the NSMutableArray *images inside the IKImageBrowser... below is the datasource section of the code snip from apple's ImageBrowser example:

    Code:
    #pragma mark IKImageBrowser DataSource
    
    - (int)numberOfItemsInImageBrowser:(IKImageBrowserView*)view
    	{
    	return [images count];
    	}
    
    - (id)imageBrowser:(IKImageBrowserView *) view itemAtIndex:(int) index
    	{
    	return [images objectAtIndex:index];
    	}
    
    - (void)imageBrowser:(IKImageBrowserView*)view removeItemsAtIndexes: (NSIndexSet*)indexes
    	{
    	[images removeObjectsAtIndexes:indexes];
    	}
    
    - (BOOL)imageBrowser:(IKImageBrowserView*)view moveItemsAtIndexes: (NSIndexSet*)indexes toIndex:(unsigned int)destinationIndex
    	{
    	NSInteger index;
    	NSMutableArray *temporaryArray;
    
    	temporaryArray = [[[NSMutableArray alloc] init] autorelease];
    
    	for (index = [indexes lastIndex]; index != NSNotFound; index = [indexes indexLessThanIndex:index])
    		{
    		if (index < destinationIndex)
    			{
    			destinationIndex --;
    			}
    			
    		id obj = [images objectAtIndex:index];
    		[temporaryArray addObject:obj];
    		[images removeObjectAtIndex:index];
    		}
    
    	NSInteger n = [temporaryArray count];
    	for (index = 0; index < n; index++)
    		{
    		[images insertObject:[temporaryArray objectAtIndex:index] atIndex:destinationIndex];
    		}
    
    	return YES;
    	}
    	
    - (void)updateDatasource
    	{
    	[images addObjectsFromArray:importedImages];
    	[importedImages removeAllObjects];
    	[imageBrowser reloadData];
    	}
    
    and so now i'm trying to somehow call on the index with my IBAction, and removeObjectAtIndex:index whatever is selected inside the image browser. currently, i can simply press either of the delete buttons on the keyboard and it will remove all selected objects from the IKImageBrowser, so i'm assuming what i'm trying to accomplish wouldn't be very difficult if i had more experience.

    Code:
    - (IBAction)removeSwatchButton:(id)sender
    	{
    	NSIndexSet *myIndexes = [NSIndexSet indexSetWithIndex:[sender intValue]];
    	[self imageBrowser:nil removeItemsAtIndexes:myIndexes];
    	[self updateDatasource];
    	}
    
    this code doesn't return any warning or errors at compile, but it doesn't work as expected.

    if i add an object to the imagebrowser, then highlight it, then try press the IBAction-delete-button (once) i get the following error in the console:

    -[NSCFArray removeObjectsAtIndexes:]: the last index (1) must be less than the count (1).

    however, if i press the IBAction-delete-button again, the object disappears.

    i can not seem to delete numerous selections at once with the IBAction-delete-button. if i highlight a group of 3 recently added items, i have to press the IBAction-delete-button 3 times as they will only disappear one at a time (in a seemingly random order).
     
  6. caveman_uk Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #6
    It's the [sender intValue] bit that's the problem - you're getting the intValue of the delete button (which is the 'sender' object)- which is almost certainly not what you want.
     
  7. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #7
    humm... so what should it be? how can i make my IBAction...

    Code:
    - (IBAction)removeSwatchButton:(id)sender
    	{
    	NSIndexSet *myIndexes = [NSIndexSet indexSetWithIndex:[sender intValue]];
    	[self imageBrowser:nil removeItemsAtIndexes:myIndexes];
    	[self updateDatasource];
    	}
    
    ...manage the same thing as this, or call this:

    Code:
    - (void)imageBrowser:(IKImageBrowserView*)view removeItemsAtIndexes: (NSIndexSet*)indexes
    	{
    	[images removeObjectsAtIndexes:indexes];
            //"images" is an instance of NSMutableArray
    	}
    
    ?
     
  8. caveman_uk Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #8
    Try this
    Code:
    - (IBAction)removeSwatchButton:(id)sender
    	{
    	NSIndexSet *myIndexes = [imageView selectionIndexes];
    	[self imageBrowser:nil removeItemsAtIndexes:myIndexes];
    	[self updateDatasource];
    	}
    
    where imageView is the IKImageBrowserView instance in your code
     
  9. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #9
    works perfectly! thanks cavaman! :)
     

Share This Page