Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - presentModalViewController via UITabBarItem and dismissModalViewController cleanly

I have a tabBarController that I add by placing the following code into:

AppDelegate.h:

    ...
    UITabBarController IBOutlet *tabBarController;
}

@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

AppDelegate.m:

    ...
   [self.window addSubview:tabBarController.view];
   [self.window makeKeyAndVisible];
   [tabBarController setDelegate:self];

I then use the following code to present a modal barcode scanning View Controller:

- (void)tabBarController:(UITabBarController *)tbc didSelectViewController:(UIViewController *)vc {
        // Middle tab bar item in question.
        if (vc == [tabBarController.viewControllers objectAtIndex:2]) {
           ScanVC *scanView = [[ScanVC alloc] initWithNibName:@"ScanViewController" bundle:nil];

           // set properties of scanView's ivars, etc

           UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:scanView];

           [tabBarController presentModalViewController:navigationController animated:YES];
           [navigationController release];
           [scanView release];
        }
    }

When it does actually get presented I think this method isn't visually appealing, because when I dismiss the modal view I am brought back to an empty view.

A lot of barcode scanning applications or applications that simply display an image picker for example; do this quite successfully. I'm just wondering what kind of implementation they would use in order to achieve such an effect.

This is a screenshot of an application called Path, which has the exact same functionality I'm after:

alt text

I also noticed that in these applications, if you are on any other tab bar item other than the middle one let's say, and you click on the tab bar item that presents the modal view, once it gets dismissed it doesn't actually bring them back to an empty view it dismisses like normal, however the actual tab bar item that presents the modal view is never selected. I would be happy with this type of functionality if that's the only way to implement this type of effect.

Any help would be greatly appreciated as I've been stuck in this for quite some time. Also I'm not even sure whether it's the right way to put all of this code in my AppDelegate in order for the View Controller to be presented as a modal. It all seems, just, wrong.

like image 229
fulvio Avatar asked Jan 13 '11 13:01

fulvio


2 Answers

Not entirely what I'm after, but I think I can move forward from this:

http://idevrecipes.com/2010/12/16/raised-center-tab-bar-button/

like image 91
fulvio Avatar answered Oct 20 '22 11:10

fulvio


When you dismiss the modal view controller, tell the tab bar to select whatever tab was originally selected.

- (void)dismissModalViewControllerAnimated:(BOOL)animated
    {
        // do whatever you need to do when dismissing

        // savedTabIndex is an int ivar
        // tabBarController is a reference, set when showing the modal view
        [[self tabBarController] setSelectedIndex:savedTabIndex];
    }

You would have to save the original tab bar selection in a variable at the start of tabBarController:didSelectViewController:.

- (void)tabBarController:(UITabBarController *)tbc
 didSelectViewController:(UIViewController *)vc
{
    // Save the tab bar index (if it's not the photo tab)
    if ([tabBarController selectedIndex] != 3]) {
        savedTabIndex = [tabBarController selectedIndex];
    }
}

There could be mistakes in this code, I just typed it without testing.

like image 2
nevan king Avatar answered Oct 20 '22 11:10

nevan king