Today to learn other people's project source code, see such a realization function: Homepage added to shopping cart, Tabbar show the number of shopping cart .... Think of not done before, here study, record:
The implementation is as follows:
When you click on the homepage to add to the shopping operation, Tabbar's shopping cart item shows the badge of the shopping cart number.
The realization of the idea is actually very simple:
is to perform the add to cart operation, send a notification, change Tabbar shopping cart Item shows the number of badge of the shopping cart number of corners.
Note: When it is 0 o'clock, it should be set to nil, otherwise it will show 0, which is not possible.
The main code to implement is as follows:
Accept Notifications
- (void) addnotification{[ajnotification addobserver:self selector: @selector (increaseshoppingcart) Name: LfbshopcarbuynumberdidchangenotificationObject: nil];}
Actions to receive notifications- (void) increaseshoppingcart{Uiviewcontroller*SHOPPINGVC = self.childviewcontrollers[2]; Nsinteger Shoppingindex=[[Ajusershopcartool Sharedinstance]getshopcargoodsnumber]; if(Shoppingindex = =0) {ShoppingVC.tabBarItem.badgeValue=Nil; return; } shoppingVC.tabBarItem.badgeValue= [NSString stringWithFormat:@"%zd", Shoppingindex];}
Logout Notification- (void) dealloc{[ajnotification removeobserver:self];}
"Home Add to Cart, Tabbar show the number of shopping carts" implementation