Maybe I am trying to approach this the wrong way…
In my main view controller I have three buttons.
Each button causes a different UIViewController and its nib to be loaded.
I use a single method to deal with button touches:
-(IBAction)touched_menu_button:(id)sender
{
SEL selector;
UIButton *button = (UIButton *)sender;
switch (button.tag)
{
case 0:
selector = @selector(ShowA:finished:context:);
break;
case 1:
selector = @selector(ShowB:finished:context:);
break;
case 2:
selector = @selector(ShowC:finished:context:);
break;
default:
selector = @selector(ShowA:finished:context:);
break;
}
[self FadeOut:selector];
}
What’s happening here is that I am running a little animation before the new view is actually shown. I call “FadeOut” with the selector that will call the method that will show the appropriate view once the animation is done. This works fine. The routines called by the selectors look like this:
-(void)ShowA:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
A_ViewController *mvc = [[[A_ViewController alloc] init] autorelease];
mvc.delegate = self;
[self presentModalViewController:mvc animated:FALSE];
}
This works fine too.
What I would like to do is reduce this to a single method called by the selector and put all the redundant code in my “touched_menu_button” method. The callback function then would look like this:
-(void)ShowNewView:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
[self presentModalViewController:mvc animated:FALSE];
}
Obviously this requires passing a different UIViewController subclass in “mvc” depending on which view it is that I want to show. And this is where I ran into trouble. Maybe it’s that’s I’ve been programming for 12 hours straight and I’m mentally fried, but I can’t seem to figure out how to do this. I’ve tried defining class variables as void , void *, id, UIViewController * and UIViewController **. For some reason I can’t seem to make it work.
I should note that a number of approaches do work, however, they run into trouble when the view is dismissed and the autorelease process takes place. It seems that I am failing at passing the address of the pointer to the UITableView subclass in all permutations I’ve tried. The only option I can identify right now is really ugly.
Instead of passing an object to the callback, you could pass the class itself.
FadeOut:then uses the class as the context info of the animation:If you are using the context for something else, you could use a variable, as it appears you are trying to do with a view controller object. The callback then allocates the class it is given.