IPhone - presentModalViewController via UITabBarItem and cleanModalViewController cleanly

I have a tabBarController that I add by putting the following code in:

AppDelegate.h:

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

AppDelegate.m:

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

Then I use the following code to represent the scan view in barcode scan mode:

 - (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 ivars, etc UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:scanView]; [tabBarController presentModalViewController:navigationController animated:YES]; [navigationController release]; [scanView release]; } } 

When it does appear, I think this method is not visually appealing, because when I reject the modal view, I return to the empty view.

Many barcode scanning applications or applications that simply display an image picker, for example; do it quite successfully. I'm just wondering what kind of implementation they will use to achieve this effect.

This is a screenshot of an application called Path, which has the same functionality as after:

alt text

I also noticed that in these applications, if you are on any other tab item other than the average, let them say, and you click on the tab bar item that represents the modal view, after it is rejected, it doesn’t actually return them to empty views, it is rejected as normal, however the actual tab bar item that represents the modal view is never selected. I would be pleased with this type of functionality if this is the only way to implement this type of effect.

Any help would be greatly appreciated as I have been stuck with this for quite some time. Also, I'm not even sure if this is the right way to put all this code in my AppDelegate so that the View controller is presented as modal. Everything seems simple, wrong.

+7
source share
4 answers

Not quite what I need, but I think I can move on:

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

+2
source

When you reject a modal view controller, inform on the tab bar to select any tab 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 need to save the original list of tabs in a variable at the beginning of tabBarController:didSelectViewController:

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

There may be errors in this code, I just typed it without testing.

+2
source

You should not show a modal view when the user clicks on a tab bar item.

Instead, you can present a modal view from the view represented by one of the tabs.

Or, if you have only one main view and a scan view that you want to present as a modal view, you just need to use the button to present the scan view from your main view. Instead, you can use the toolbar with a single button.

+1
source

I found a very easy solution while playing around UITabBarControllerDelegate - I just tried this on iOS 7.

First, subclass UITabBarController , make it your own UITabBarControllerDelegate and create a property that will contain a link to the tab from which you want to start the modal. In my application, it is called the Sell tab.

 @property (strong, nonatomic) UIViewController *sellTab; 

Then in your init method just create this controller and add it to the tabs.

 _sellTab = [[UIViewController alloc] init]; _sellTab.title = @"Sell"; self.viewControllers = @[homeTab, historyTab, _sellTab, bookmarksTab, profileTab]; 

Now here is the magic: redefine the following delegate methods of the tab bar controller delegate. The code is pretty clear.

 #pragma mark - Tab bar controller delegate - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { return viewController != self.sellTab; } - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { if (item == self.sellTab.tabBarItem) { [self presentViewController:[[UINavigationController alloc] initWithRootViewController:[[PostAdViewController alloc] init]] animated:YES completion:nil]; } } 

This will lead to the appearance of a modality, which after the dismissal shows the same tab as before launch.

+1
source

All Articles