MHCustomTabBarController
MHCustomTabBarController copied to clipboard
How to know which tabitem is selected?
How can I know which tabitem is selected? Thanks
Had the same problem. What I did is set the tag of the buttons to "1","2" ... and add a a variable in the class (int _activeTab) Then in prepareForSegue you could do something like
UIButton * theSender = sender;
switch (theSender.tag) {
case 1:
//Child 1
_activeTab =1;
case 2:
//Child 1
_activeTab =2;
....
A bit dirty as I had to change class but working fine for me.. Cheers, Oggerschummer
Hi everyone, I was just thinking about it and I believe it will not be a good idea to expose a "selectedIndex" property because the storyboard setup can be rather complex so that an index may have no meaning. But I think it will be a good idea to add a "selectedButton" property, so we can manually check if a specific button has been selected. I plan to add this today or tomorrow. Thanks!
Hi, agreed this may be a better solution,except that the ordering of the child view controllers is then unclear.
I suggest that you as well exponse methods like "showNext" and "showPrevious" plus "showFirst"/"ShowLast" that can be used to trigger switching the controllers from outside. For my project I had to support swipe gestures to navigate between the child controllers. I did the trick using the above hack plus adding to the derived view controller something hackish like
- (IBAction)swipedLeft:(UISwipeGestureRecognizer*)sender{
if (_activeTab == 4){
return;
} else
{
UIButton * theFakeSender;
switch (_activeTab) {
case 1:
theFakeSender=_button2;
break;
case 2:
theFakeSender=_button3;
break;
case 3:
theFakeSender=_button4;
break;
default:
break;
}
[self performSegueWithIdentifier:[NSString stringWithFormat:@"viewController%i",++_activeTab ] sender:theFakeSender];
}
}
(I have 4 children, so swiping further than controller 4 is canceled) So such an index may still be useful. Another solution would be to have an internal orderer array holding the buttons that can be populated from the child class to define the order in which they should be used.
Just my little thoughts, there are plenty solutions possible.
The "showNext" and "showPrevious" features would then depend on the ordering of the internal used IBOutletCollection. There is no way to test the ordering of the collection and to check that it's always the way how the user wants it. It would also require that the buttons are added to the collection in the correct order. I'm not really sure if that is such a good idea.
As there is the need to name the segues with "viewController1"… “viewControllerN” there is already some order defined. Anyhow, I implemented on my local version, so that´s fine with me.
On 16 Jul 2015, at 08:33, Martin Hartl [email protected] wrote:
The "showNext" and "showPrevious" features would then depend on the ordering of the internal used IBOutletCollection. There is no way to test the ordering of the collection and to check that it's always the way how the user wants it. It would also require that the buttons are added to the collection in the correct order. I'm not really sure if that is such a good idea.
— Reply to this email directly or view it on GitHub https://github.com/mhaddl/MHCustomTabBarController/issues/19#issuecomment-121846341.