开发者

Using the same pointer to point to different UIViewControllers

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:m开发者_开发问答vc 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.

-(IBAction)touched_menu_button:(id)sender {
    Class theClass;

    UIButton *button = (UIButton *)sender;

    switch (button.tag) {
        case 0:
            theClass = [A_ViewController class];
            break;

        case 1:
            theClass = [B_ViewController class];
            break;

        case 2:
            theClass = [C_ViewController class];
            break;

        default:
            theClass = [A_ViewController class];
            break;
    }

    [self FadeOut:theClass];
}

FadeOut: then uses the class as the context info of the animation:

- (void)FadeOut:(Class)theClass {
    //...
    [UIView beginAnimations:@"FadeOut" context:(void*)theClass];
    //...
    [UIView commitAnimations];
    //...
 }

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.

-(void)ShowA:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    Class theClass = (Class)context; // or get it from a variable if you don't use the context
    // If the view controllers all have a shared superclass which declares the delegate property, you can specify that as the type and use dot notation, but otherwise you will need to call setDelegate:
    UIViewController *mvc = [[[theClass alloc] init] autorelease];
    [mvc setDelegate:self];
    [self presentModalViewController:mvc animated:FALSE];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜