RunTime---Setting the badgeview of Tabbarbutton

Source: Internet
Author: User

1. Get the member variables of the class--class_copyivarlist

if([Badgeviewchild iskindofclass:nsclassfromstring (@"_uibadgebackground")]) {  //NSLog (@ "Finally found you, fortunately did not give up");UnsignedintCount//after getting finished, the value of count is the number of member variables on the current class of code//get member variables on a classIvar *vars = class_copyivarlist (nsclassfromstring (@"_uibadgebackground"), &count); //Traverse View   for(intI=0; i<count; i++) {Ivar var=Vars[i]; //get the name of VarNSString *name =[NSString Stringwithcstring:ivar_getname (Var) encoding:nsutf8stringencoding]; //Get TypeNSString *type =[NSString stringwithcstring:ivar_gettypeencoding (Var) encoding:nsutf8stringencoding]; NSLog (@"%@=====%@", Name,type);       if([Name isequaltostring:@"_image"]) {          //assigning values via KVC[Badgeviewchild setvalue:[uiimage imageNamed:self.badgeImageName] forkeypath:name]; }    }  //Freeing MemoryFree (VARs);
}

2, method Exchange and the original method after the call to do their own logic

+ (void) load{Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{Class Clazz= [Selfclass]; //get the original method with the SEL that needs to interactSEL Originalselector =@selector (setbadgevalue:); SEL Swizzledselector=@selector (wb_setbadgevalue:); //get the method on the class by SelMethod Originalmethod =Class_getinstancemethod (Clazz, originalselector); Method Swizzledmethod=Class_getinstancemethod (Clazz, swizzledselector); //add a method to a classBOOL result =Class_addmethod (Clazz, Originalselector, Method_getimplementation (Swizzledmethod), method_gettypeencoding (                Swizzledmethod)); if(Result) {//If you add success, replace the method implementationClass_replacemethod (Clazz, Swizzledselector, Method_getimplementation (Originalmethod), method_getTypeEncod        ING (Originalmethod)); }Else{            //no added success, direct Exchange method implementationmethod_exchangeimplementations (Originalmethod, Swizzledmethod); }    });}

3. Object Association--to set an association object to any NSObject object--to solve the classification, add a new property to the Get and set method (the method to add attributes to the classification)

// Set association Properties -(void) Setbadgeimagename: (NSString *) badgeimagename{    @ "haha  ", Badgeimagename, objc_association_copy_nonatomic);} -(NSString *) badgeimagename{    return@ "haha");}

Modify the display of the badgevalue displayed by the system

Implementation: Adding Categories to Uitabbaritem (badgevalueimage)

The specific implementation is as follows:

UITabBarItem.h
// //   uitabbaritem+badgevalueimage.h////  Created by Ji on 15/8/20. //   Copyright (c) 2015 Ji. All rights reserved. // #import <UIKit/UIKit.h>@interface*badgeimagename; @end
Uitabbaritem.m
////UITABBARITEM+BADGEVALUEIMAGE.M////Created by Ji on 15/8/20.//Copyright (c) 2015 Ji. All rights reserved.//#import "uitabbaritem+badgevalueimage.h"#import<objc/runtime.h>@implementationUitabbaritem (badgevalueimage)+ (void) load{Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{Class Clazz= [Selfclass]; //get the original method with the SEL that needs to interactSEL Originalselector =@selector (setbadgevalue:); SEL Swizzledselector=@selector (wb_setbadgevalue:); //get the method on the class by SelMethod Originalmethod =Class_getinstancemethod (Clazz, originalselector); Method Swizzledmethod=Class_getinstancemethod (Clazz, swizzledselector); //add a method to a classBOOL result =Class_addmethod (Clazz, Originalselector, Method_getimplementation (Swizzledmethod), method_gettypeencoding (                Swizzledmethod)); if(Result) {//If you add success, replace the method implementationClass_replacemethod (Clazz, Swizzledselector, Method_getimplementation (Originalmethod), method_getTypeEncod        ING (Originalmethod)); }Else{            //no added success, direct Exchange method implementationmethod_exchangeimplementations (Originalmethod, Swizzledmethod); }    });}- (void) Wb_setbadgevalue: (NSString *) badgevalue{//calling the parent class[self wb_setbadgevalue:badgevalue]; //Implementing Logic//if Badgevalue is nil. Direct return    if(!badgevalue) {        return; }        //What we can get directly is Wbtabbar.Uitabbarcontroller *target = [Self Valueforkeypath:@"_target"];  for(UIView *tabbarchildinchtarget.tabBar.subviews) {//Looking for Uitabbarbutton        if([Tabbarchild iskindofclass:nsclassfromstring (@"Uitabbarbutton")]) {             for(UIView *tabbarbuttonchildinchtabbarchild.subviews) {if([Tabbarbuttonchild iskindofclass:nsclassfromstring (@"_uibadgeview")]) {                     for(UIView *badgeviewchildinchtabbarbuttonchild.subviews) {if([Badgeviewchild iskindofclass:nsclassfromstring (@"_uibadgebackground")]) {                            //NSLog (@ "Finally found you, fortunately did not give up");UnsignedintCount//after getting finished, the value of count is the number of member variables on the current class of code//get member variables on a classIvar *vars = class_copyivarlist (nsclassfromstring (@"_uibadgebackground"), &count); //Traverse View                             for(intI=0; i<count; i++) {Ivar var=Vars[i]; //get the name of VarNSString *name =[NSString Stringwithcstring:ivar_getname (Var) encoding:nsutf8stringencoding]; //Get TypeNSString *type =[NSString stringwithcstring:ivar_gettypeencoding (Var) encoding:nsutf8stringencoding]; NSLog (@"%@=====%@", Name,type); //                                if([Name isequaltostring:@"_image"]) {                                    //assigning values via KVC[Badgeviewchild setvalue:[uiimage ImageNamed:self.badgeImageName] forkeypath:name]                                ; }                            }                            //Freeing MemoryFree (VARs); }                    }                }            }        }    }}//Setting Association Properties- (void) Setbadgeimagename: (NSString *) badgeimagename{objc_setassociatedobject (self,@"haha", Badgeimagename, objc_association_copy_nonatomic);}-(NSString *) badgeimagename{returnObjc_getassociatedobject (Self,@"haha");}@end

RunTime---Setting the badgeview of Tabbarbutton

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.