1

I am NOT using a UITabBarController.

I have a view controller and I added a UITabBar, it has two items.

How can I make sure that when the first item is clicked, it loads the content from a specific view controller, and when another item is clicked, loads the content from another view controller?

Yann Bodson
  • 1,634
  • 1
  • 17
  • 29
sarah
  • 1,201
  • 1
  • 9
  • 29
  • how u added the tab bar ? code or storyboard ? show some code of what you tried – Teja Nandamuri Dec 12 '15 at 23:20
  • @Mr.T i add the tab bar by drag and drop. i don't have code for it, i just can say that i make an outlet for the uitabbar, and then for that instance, i set the delegate, and i can catch that the item in the uitabbar is being clicked but my issue is that i don't know how to load a view controller when an item inside the uitabbar is clicked. i don't face that problem normally with uitabbarcontroller, but now i am facing it because i am using just uitabbar – sarah Dec 12 '15 at 23:23
  • did u connect the view controller to the tab bar item ? – Teja Nandamuri Dec 12 '15 at 23:23
  • Curious - what you describe is exactly what `UITabBarController` is for. Why not use one? – rmaddy Dec 13 '15 at 01:25
  • 1
    @rmaddy because i want the uibar to be in the top, now i am still with the problem any hep please? – sarah Dec 13 '15 at 10:39
  • @Mr.T yes you can't make a segue – sarah Dec 13 '15 at 10:39
  • help please guys, i am out of solutions – sarah Dec 13 '15 at 23:35

2 Answers2

0

As mentioned in other answers (Detect when a tab bar item is pressed), You should implement the UITabBarDelegate protocol.

Once you're implementing this protocol, just use the - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item method to know when a user changes the selected item of a tab bar. implement this method, and you will have to check which item is selected, and manually change your view's content based on that.

yonivav
  • 994
  • 11
  • 18
-1

You need to set the delegate of the UITabBar to be the UIViewController and the UIViewController needs to conform to the UITabBarDelegate protocol and implement the method

Swift

optional func tabBar(_ tabBar: UITabBar, didEndCustomizingItems items: [UITabBarItem], changed changed: Bool)

Objective-C

- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed

You can find details in the UITabBarDelegate documentation

LunaCodeGirl
  • 5,432
  • 6
  • 30
  • 36
  • 1
    did you even read the question ? ? ? ?? this has nothing to do with the content that iwant to load – sarah Dec 13 '15 at 10:41
  • @sarah excuse me for trying to help. I don't really feel like my not understanding which thing you were unsure of deserves a down vote. Yes, I read your question, it's not very clear. For example, why aren't you using a UITabBarController? I thought you were asking about how to receive tap events from the UITabBar. I guess I don't understand at all what your trying to do. Can you explain what the desired end result would be? – LunaCodeGirl Dec 13 '15 at 17:28
  • first of all, it's not me who downvote, i don't have a good reputation to downvote. second, i've my reasons why i don't want to use UITabBarController. third, if you didn't understand my question, i guess it would be better to make a comment and then i would explain more. fourth. i am asking about how to load a view controller when a user clicks on the tab – sarah Dec 13 '15 at 18:53
  • What do you mean by "Load a view controller"? If you want you can just present one view controller from the other. If you're in a navigation stack you can push a view controller onto the stack (http://www.idev101.com/code/User_Interface/UINavigationController/pushPop.html). If you're in a container view controller you can swap out the displayed view controller (https://www.objc.io/issues/1-view-controllers/containment-view-controller/). Outside of those options I think if neither works you might not be understanding UIViewControllers correctly. If you can give more details that would help. – LunaCodeGirl Dec 14 '15 at 06:28
  • I can't present a view controller completely, because that would mean i show a completely new view controller, what i want is when a user clicks on a tab bar item, a view controller shold be shown **inside** the view controller that has the tab bar. think of it as uitabbar controller, when u click on a bar item, a new content show **inside** the same view controller – sarah Dec 14 '15 at 11:18
  • You need to use a container view controller, read the Objc.io article https://www.objc.io/issues/1-view-controllers/containment-view-controller/ and you should find your answer. – LunaCodeGirl Dec 14 '15 at 11:59
  • please don't u have a swift version ? that language for me is like Spanish :) :) i don't understand nothing – sarah Dec 15 '15 at 10:07