Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4272960
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:38:54+00:00 2026-05-21T07:38:54+00:00

Maybe I am trying to approach this the wrong way… In my main view

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T07:38:55+00:00Added an answer on May 21, 2026 at 7:38 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

maybe it's not so proper to ask this question here... anyway, I'm trying to
I've been trying this for hours, so maybe a fresh set of eyes will
I am trying to solve this problem : https://www.spoj.pl/problems/CERC07S/ I have identified that i
Unsatisfied with my current approach I'm just trying to redesign the way I build
Maybe dumb question: I'm trying to realize a little server in Java with com.sun.net.httpserver
I'm trying to learn JavaFX and maybe create a few learner games. I always
Maybe I'm just an idiot, but I've been trying to implement a game loop
Maybe my question it's a little weird, but I'm trying to build a not-so-simple
I'm really trying to understand the difference between OpenID and OAuth? Maybe they're two
I'm trying to do multithread uploads, but get errors. I guessed that maybe it's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.