IOS Uitabbarcontroller records the last selected TabIndex and returns

Source: Internet
Author: User

Uitabbarcontroller can implement multiple Uiviewcontroller independent displays, but the control of the relationships between the Uiviewcontroller is not very well controlled, especially when the most recently selected tab number is not recorded. Some requirements are more difficult to implement, such as under a tab through an event can be switched to the last selected b tab, or a tab in the event needs to switch to b tab and after the B-tab processing is complete can return to a tab. You can then meet this requirement if you know the tab number that Uitabbarcontroller last checked.


Reference Code

The code is as follows Copy Code
MyTabBarController.h
@interface mytabbarcontroller:uitabbarcontroller<uitabbarcontrollerdelegate> {
The last selected index
Nsuinteger _lastselectedindex;
}

@property (readonly, nonatomic) Nsuinteger Lastselectedindex;
@end

Mytabbarcontroller.m
@implementation Mytabbarcontroller
@synthesize Lastselectedindex=_lastselectedindex;

-(void) setSelectedIndex: (Nsuinteger) SelectedIndex
{
To determine whether equality, different before setting
if (Self.selectedindex!= selectedindex) {
Set the last time
_lastselectedindex = Self.selectedindex;
NSLog (@ "1 old:%d, new:%d", Self.lastselectedindex,selectedindex);
}

Invoke the setSelectedIndex of the parent class
[Super Setselectedindex:selectedindex];
}

-(void) Tabbar: (Uitabbar *) Tabbar Didselectitem: (Uitabbaritem *) Item
{
Get the selected item
Nsuinteger TabIndex = [Tabbar.items Indexofobject:item];
if (TabIndex!= self.selectedindex) {
Set last Change
_lastselectedindex = Self.selectedindex;
NSLog (@ "2 old:%d, new:%d", Self.lastselectedindex,tabindex);
}
}

@end

Implementations require simultaneous display-(void) setSelectedIndex: (Nsuinteger) SelectedIndex and – (void) Tabbar: (Uitabbar *) Tabbar Didselectitem: ( Uitabbaritem *) Item These two methods, by adding a readonly Lastselectedindex

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.