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 8510671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:46:08+00:00 2026-06-11T03:46:08+00:00

I have an educational app working fine with tab bars. One of the tabs

  • 0

I have an educational app working fine with tab bars. One of the tabs is a test for the user. If a user taking the test selects another tab, I would like an action sheet to display to confirm they really want to exit the test since their test results will be lost.

I can’t get the action sheet to display. I am getting syntax errors with the first three examples below. The 4th example will compile OK but the app aborts. I can’t figure out what should go after the self. Or possibly one of the other examples would work if I had the syntax correct.

    - (void)tabBarController:(UITabBarController *)tabBarController 
didSelectViewController:(UIViewController *)viewController {
NSLog(@"Tab Bar Controller reached");

UIActionSheet *actionSheet = [[UIActionSheet alloc]
                              initWithTitle:@"This will end the test.  
                              Are you sure you want to leave?"
                              delegate:self
                              cancelButtonTitle:@"Cancel"
                              destructiveButtonTitle:@"Yes, 
                              cancel the test."
                              otherButtonTitles:nil];
[actionSheet showInView:self.view];   
[actionSheet showInView:elements1AppDelegate.window];
[actionSheet showFromTabBar:self.tabBarController.tabBar];
[actionSheet showFromTabBar:self];

Ultimately, once I get the action sheet to display, I will either proceed to the tab selected or stay in the test view depending on whether the user decides to exit or not. Any help/suggestions would be appreciated. Thanks.

  • 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-06-11T03:46:09+00:00Added an answer on June 11, 2026 at 3:46 am

    The name of the method tabBarController:didSelectViewController: should indicate that it’s too late to stop the selection. Its return type void indicates there is not much you can do about it. Instead focus on method names that have “will” or “should” in them, and return types like BOOLs such as tabBarController:shouldSelectViewController:. So here is some basic code that does what you want.

    I don’t know the actual classname of your test’s view controller so I’ll use QuizController as a classname. QuizController is a UIViewController subclass.

    QuizController.m

    @interface QuizController () <UITabBarControllerDelegate,UIActionSheetDelegate>{
        BOOL _testInProgress;
    }
    @end
    @implementation QuizController
    -(void)viewDidAppear:(BOOL)animated{
        [super viewDidAppear:animated];
        // When a tabs view controller is presented viewDidAppear: will be called so here we will set this view controller as the tabBarController delegate so we get the callback.
        self.tabBarController.delegate = self;
    }
    -(void)startQuiz{
        _testInProgress = YES;
        // Begin testing code
    }
    -(void)stopQuiz{
        // Score test record results
        _testInProgress = NO;
    }
    -(void)cancelQuiz{
        // Throw out results
        _testInProgress = NO;
    }
    -(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
        if (!_testInProgress) return YES;
        // If trying to select this controller then who cares?
        if (viewController == self) return YES; // Or NO. Just don't show the sheet.
        UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"You are in the middle of a test. Are you sure you want to switch tabs?"
                                                            delegate:self
                                                   cancelButtonTitle:@"Continue Test"
                                              destructiveButtonTitle:@"Abort Test"
                                                   otherButtonTitles:nil];
        // Lets cheat and use the tag to store the index of the desired view controller.
        action.tag = [self.tabBarController.viewControllers indexOfObject:viewController];
        [action showFromTabBar:self.tabBarController.tabBar];
        return NO;
    }
    -(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
        if (buttonIndex == actionSheet.destructiveButtonIndex){
            [self cancelQuiz];
            // The above cheat pays off.
            [self.tabBarController setSelectedIndex:actionSheet.tag];
        }
    }
    

    EDIT (In response to comment quoted)

    I’m a little confused about your example. Currently, my “take test”
    class is a UIViewController. Are you suggesting I replace that with
    your QuizController above?

    No. I am suggesting that you take this code and integrate the design pattern into your UIViewController subclass that handles your test. Although this is a working example (providing you supply UI to toggle the _testInProgress ivar.)

    Do I leave my current UITabBarController in place?

    Yup.

    That is currently my appdelegate and rootController.

    Huh? Your UITabBarController is almost certainly your rootViewController. But unless you have done something very odd like AppDelegate : UITabBarController <UIApplicationDelegate>, by the way don’t do that, then it is extremely unlikely that your “appdelegate” and your UITabBarController are the same. Much more likely your AppDelegate is your tabBarController‘s delegate.

    If just setting the tabBarController.delegate property on appearance is bad (and it could very well be), i.e. some other object needs to be the tabBarController‘s delegate, then you’ll have to forward a message to that view controller to see if a test is in progress. For this you could actually leave almost all of the code in the example unchanged. Of course you would have to remove the self.tabBarController.delegate = self; in viewDidAppear:. And put the following in your AppDelegate(presuming that’s the tabBarController‘s delegate):

    -(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
        if ([tabBarController.selectedViewController respondsToSelector:@selector(tabBarController:shouldSelectViewController:)]){
            return [(NSObject <UITabBarControllerDelegate>*)tabBarController.selectedViewController tabBarController:tabBarController shouldSelectViewController:viewController];
        }
        return YES;
    }
    

    This implementation essentially forwards the responsibility to answer the question to the view controller, provided it will answer the question.

    I check in there if the “take test” tab was selected and call an
    initialization method in my “take test” class.

    In my opinion the “take test” view controller should simply become selected when the user taps the tab for it. And its view should contain a button with something to the effect of ‘start test’ on it. But that’s just my opinion.

    But whatever the case is the application’s knowledge of whether the user is taking a test should reside in the view controller that administers the test.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have integrated the facebook in my app using http://sourceforge.net/projects/facebook-bb-sdk , it work fine
Background I'm working on a an educational JavaScript application/site (SPA) that will eventually have
I am working in a small educational project where we have to implement a
I am working on a educational related apps. My app functionality is working well
I have an app that needs to gain access to a user's friends' information.
I have to create an educational app where it is basically like a powerpoint
I am uploading a Free 'Educational' app on Samsung App store. I have filled
I'm trying to build an educational app that will have approximately 3-5 completely different
Hello I am working on an educational app just for my own learning where
I'm working on a educational app involving complex scripts in which I paint parts

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.