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