View Full Version : Calling a void function from an IBAction?
Darkroom
Nov 3, 2008, 06:11 PM
not sure how to do this properly, although i believe it's easy (or maybe it's more complex then i assume)...
- (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.
Krevnik
Nov 3, 2008, 06:53 PM
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?
lee1210
Nov 3, 2008, 06:56 PM
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.
NSIndexSet *myIndexes = [NSIndexSet indexSetWithIndex:[sender intValue]];
[self imageBrowser:nil removeItemsAtIndexes:myIndexes];
would work.
[self imageBrowser:nil removeItemsAtIndexes:nil];
should work also, but... doesn't make much sense.
-Lee
Darkroom
Nov 3, 2008, 07:21 PM
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.
NSIndexSet *myIndexes = [NSIndexSet indexSetWithIndex:[sender intValue]];
[self imageBrowser:nil removeItemsAtIndexes:myIndexes];
would work.
[self imageBrowser:nil removeItemsAtIndexes:nil];
should work also, but... doesn't make much sense.
-Lee
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...
Darkroom
Nov 3, 2008, 11:15 PM
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?
the index is indexing the NSMutableArray *images inside the IKImageBrowser... below is the datasource section of the code snip from apple's ImageBrowser example:
#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.
- (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).
caveman_uk
Nov 4, 2008, 07:06 AM
- (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).
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.
Darkroom
Nov 4, 2008, 01:42 PM
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.
humm... so what should it be? how can i make my IBAction...
- (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:
- (void)imageBrowser:(IKImageBrowserView*)view removeItemsAtIndexes: (NSIndexSet*)indexes
{
[images removeObjectsAtIndexes:indexes];
//"images" is an instance of NSMutableArray
}
?
caveman_uk
Nov 4, 2008, 03:19 PM
Try this
- (IBAction)removeSwatchButton:(id)sender
{
NSIndexSet *myIndexes = [imageView selectionIndexes];
[self imageBrowser:nil removeItemsAtIndexes:myIndexes];
[self updateDatasource];
}
where imageView is the IKImageBrowserView instance in your code
Darkroom
Nov 4, 2008, 03:26 PM
Try this
- (IBAction)removeSwatchButton:(id)sender
{
NSIndexSet *myIndexes = [imageView selectionIndexes];
[self imageBrowser:nil removeItemsAtIndexes:myIndexes];
[self updateDatasource];
}
where imageView is the IKImageBrowserView instance in your code
works perfectly! thanks cavaman! :)
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.