views:

26

answers:

3

Alternatively, I could use something like viewWillAppear, only switching tabs doesn't call viewWillAppear - IF I can access selectedItem or selectedIndex reliably from there.

The goal is to re-use a similar table view, with 3 tabs filling the table with differently filtered data.

I tried overriding didSelect and using the app delegate as UITabBarDelegate, but got the error 'Changing the delegate of a tab bar managed by a tab bar controller is not allowed.'

The tab bar controller, rootCt, is in the app delegate and works correctly.

So that's the trick I'm looking for - getting a notification from the root (tab bar) controller when the index has changed. Ideas?

A: 
tabBarController.tabBar.selectedItem.tag

It will give you the tag of current selected tabbar index

If you are using tabBar den

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item

is the delegate method that gets called when tabbar is selected.

happy iCoding...

Suriya
No, I need to execute some code when the change happens. Since one can navigate on to misc. other views within each tab, I need to perform some 'resetting' or initializing code to make sure certain things are restored, data is refreshed, view is presentable, that kind of thing. Thanks anyway, made the headline more clear. Any solution?
Henrik Erlandsson
Are you using tabbar or tabbarController? Edited my answer for better understanding
Suriya
A: 

Try implementing an UITabBarController delegate. It has a method similar to the didSelect: method offered by UITabBar delegate:
tabBarController:didSelectViewController:. It will be called after the user has selected another tab.

See: UITabBarControllerDelegate Protocol Reference

Toastor
Yes, that worked. It was easy to make the app delegate a UITabBarControllerDelegate, and the first time I click on a previously un-viewed tab, selectedIndex is correct.
Henrik Erlandsson
I made a big mistake, namely to press Enter while typing a comment. The above comment can no longer be edited, said the forum after I'd typed the rest of it. Luckily I could copy the comment as it should be.Yes, that worked. It was easy to make the app delegate a UITabBarControllerDelegate, and the first time I click on a previously un-viewed tab, selectedIndex is correct. However - if I click tab 2, then tab 3, then tab 2 again, viewWillAppear for that view is called, but the didSelectViewController method is not - and selectedIndex is not changed! Is there another way to get a correct index?
Henrik Erlandsson
A: 

I don't usually do this kind of thing (answer my own question). But here goes.

For example, if I click tab 2, then tab 3, then tab 2 again, viewWillAppear for that view is called, but the didSelectViewController method is not - and selectedIndex is not changed!

It appears as if selectedIndex is only updated if a view is loaded, not if the view is already loaded and simply appears.

I did some testing, and unlike selectedIndex, the tab bar's selectedItem is correctly updated (in viewWillAppear for the view in the clicked tab) even if the view is already loaded. By putting f.ex. the tabs' titles in an array, the matching index can be looked up.

So I will omit the didSelectViewController and won't need a UITabBarController, I only need to connect the UITabBar to an IBOutlet and use [myTabBar selectedItem].title to initialize correctly in the re-used view's viewWillAppear.

If someone offers a more generic/useful/simple solution I'll gladly mark that! Will check back in a few days and mark this if not. Just glad I made it work :)

Henrik Erlandsson