4

what i want is suppose to be simple, instagram middle tab on IOS opens the app's camera interface but not like the rest of the tabs but as an independet view controller and when you press cancel it gets back to the last tab youv'e been.

any ideas??

aviv_elk
  • 396
  • 1
  • 6
  • 21

3 Answers3

7

Edit: In order for this code to work you'll need to subclass UITabBarController and implement the UITabBarDelegate. So you'll add something like this:

@interface MyTabsViewController : UITabBarController <UITabBarDelegate>

In Interface Builder you'll need to set the tags of your tab items to whatever you need them to be:

enter image description here

And now you can do something like this:

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if(item.tag == 1)
    {
        [self presentViewController:myVC animated:YES completion:nil];
    }
    else if(item.tag == 2)
    {
       //your code
    }
}

This will allow you to get the tapped event for each of your tab bar items and do add some custom code, you just need to add the appropriate tags to your tab buttons in interface builder. For example, you could add code to show your own view controller as shown above.

Hope that helps!

And here's another link: How can i get Tabbar Item Button's Click event by clicking on TabbarItem button?

Community
  • 1
  • 1
erparker
  • 1,301
  • 10
  • 20
  • thanks, right now i didnt implemented my own custom UITabBarController but i created the tabs in the storyboard, so my question is, do i need to implement the code you posted in my own custom UITabBarController or can i put that code in one of my view controllers or app delegate or something? thank tou. – aviv_elk May 21 '15 at 18:19
  • 1
    Edited my answer to give some more detail. You'll need to subclass UITabBarController and set the tag of each tab item in IB. You'll also need to implement the UITabBarDelegate protocol so you can have access to the delegate method tabBar:didSelectItem: – erparker May 22 '15 at 16:14
1

Sounds like what you're trying to do involves a TabBarViewController. You can start a project off by choosing this option from the Xcode welcome screen to create a new project:

enter image description here

enter image description here

This "Tabbed Application" starter project will give you a fully functional and running app you can build from.

The way you're describing the tab bar controller in Instagram sounds like you'll have to do a couple customizations to your tab bar controller. First, the camera tab in the middle has a blue background. Try this SO article to help with that.

When you hit the cancel button on the camera page and you want the last selected tab to be selected use this method as a starting place to programatically select a certain tab:

[self.tabBarController setSelectedIndex:2];

Hope this gets you started in the right direction.

Community
  • 1
  • 1
Ethan Parker
  • 2,896
  • 1
  • 21
  • 27
  • thank you for your answer but i already have a running tab bar but i think im suppose to use the event of the selected tab and on that event it suppose to do somthing like : [self.navigationController pushViewController:cameraViewController animated:YES]; – aviv_elk May 21 '15 at 16:25
  • Are you having trouble getting the camera page to show, is that the main problem? – Ethan Parker May 21 '15 at 16:28
0

SWIFT 4 Code

After subclassing the Tabbarcontroller as specified by @erparker above, use this code

 override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        if(item.tag==300){


            if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "addNewTaskView") as? AddNewTaskViewController {

               self.present(viewController, animated: true, completion: nil)
            }

        }
    }

here, "Main" is the storyboard name which contains target view controller and "addNewTaskView" is the view controller storyboard Id given at the storyboard for a view controller.

ZameerMoh
  • 990
  • 1
  • 13
  • 24