Cant set the Delegate of my UITabbar - ios

I have a simple TabBar-Controller in my Storyboard.
ViewController 1 has a Navigation Controller around itself.
ViewController 2 does not have one.
ViewController 1 is shown first when the App goes up.
i need to implement the following Method:
-(void)tabBar:(UITabBar *)tabBar didSelectItem(UITabBarItem *)item
But i cant set the Delegate of the UITabBar. I Want to set the Deleagte in ViewController 1. In IB i can show the delegate property but i cant draw a line. i tried to set the delegate programmatically. But no matter where i set it, the app always crashes.
So where do i have to set the delegate of my UITabBar ?

You should make the App Delegate the delegate of your tab bar. You should be able to drag from the tab bar controller in storyboard to the AppDelegate (maybe file's owner).
In code, you can get a reference to your tab bar controller and set tbc.delegate = self in didFinishLaunchingWithOptions:.
In both scenarios, make sure to first make the delegate listen to the delegate methods by adding <UITabBarControllerDelegate> to the interface declaration.

I found the solution for this problem. All i did was the following, i created a class for my UITabBarController. I put the Protocol in to the header file. Switched to the .m and implemented the
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item Method.
And that's it. There is no need to set the delegate explicitly.
The delegate-Method is called now.

Related

Check if TabBar Item has been selected

I'm new in iOS programming, I'm wondering if there is a way to use TabBar inside normal view instead of UITabBarController.
This is my scenario:
I've a UIViewController with a TabBar and a Container. Container is embedded to a UIPageViewController to show different pages. I made this due to easy way to control layout in storyboard. The problem is that I don't know how to use tabBarController:didSelectViewController: function with this setup. I'm able to use it in normal UITabBarController but I want to force my custom view to works! This is my setup:
Storyboard Setup
Is there a way to catch a tab item selected action with this setup?
I found the solution. It was so simple!
Just setted self.myTabBar.delegate = self; and <UITabBarDelegate> and using -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item instead of tabBarController's one.

Xcode iOS: UITabBar update objects while switching to another tab

I want an action, that starts when the user is switching the tabs in a UITabBarController so I can update the labels and stuff on the View the user is switching to. Do you guys know any action, that does this thing.
Thanks.
The following method is called, when a tab is switched in UITabBar:
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
This is delegate method of UITabBarDelegate protocol.

viewWillDisappear is not called

I'm using a navigationController in my app. One UI i have tried to click one button (Not in Navigation bar) viewWillDisappear is not called. But i'm not using the navigationController it's calling the viewWillDisappear. Can you please help me how to call the viewWillDisappear with a navigationController?
So, first of all - (void)viewWillDisappear:(BOOL)animated is a method specific to UIViewController class.
Second, this method shouldn't be called directly from the code, this method is called automatically when a UIViewController is removed from a view hierarchy. (check UIViewController specs)
So in order to handle this method call, you have to implement it in your custom UIViewController class (do not forget to call [super viewWillDisappear:animated]). Whenever your custom UIViewController view will disappear from the screen (is popped from the stack or other UIViewController is added to the stack) this method will be called.

How to Set the Selected Item in a UITabBar From within the Code?

I'm implementing UITabBar in my app. I managed making it work by implementing UITabBarDelegate in my header file and using
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
in my .m file (as explained in this tutorial).
Now, there are 3 scenarios in my app where I wish to set the selected UITabBarItem manually from within the code (and not based on user action):
Upon viewDidLoad
After didReceiveMemoryWarning
In a certain case when the user is entering another view controller - when they get back, they should get back to a different tab than the one they clicked on.
Can anyone direct me to how this should be done?
The UITabBarController class has two properties for managing the selected tab, namely, selectedViewController and selectedIndex. Look into those in the reference.

iOS TabBarController / inits ViewControllers by which init-method?

i currently wonder which init-Method is called in the ViewController via TabBarController on tapping a tab.
i tested
init, initWithStyle, initWithFrame, initWithNibName:andbundle..
but non if them seem to be called.
Any idea, which one is the correct?
for logical reasons i dont want to just use viewDidAppear/Load here..
Any clue?
thanks, Daniel
it's - (id)initWithCoder:(NSCoder *)aDecoder
But this happens before the tab is tapped.
initWithCoder: is called when the viewController is added to the UITabBarController.
If you want to respond to a tap on the tab you have to use viewWillAppear: viewDidAppear: or viewDidLoad

Resources