即时改变badgeValue的值(使用KVO)

本文介绍了一个使用Objective-C编写的iOS应用程序示例,通过UITabBarController实现多视图导航,并利用KVC观察者模式更新TabBar徽标值。

首先先创建一个类

Number

@interface Number : NSObject
{
    NSNumber *badgeValue_number;    //属性
}
@end


YcwAppDelegate.h文件内

@property (retain, nonatomic)Number *ycwNumber;


YcwAppDelegate.m文件内

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    
    YcwViewController *view = [[YcwViewController alloc]initWithNibName:@"YcwViewController" bundle:nil];
    YcwSecondViewController *secondView = [[YcwSecondViewController alloc]initWithNibName:@"YcwSecondViewController" bundle:nil];
    
    YcwThirdViewController *thirdView = [[YcwThirdViewController alloc]initWithNibName:@"YcwThirdViewController" bundle:nil];
    NSArray *array_controllers =[NSArray arrayWithObjects:view,secondView,thirdView, nil];
    
    UITabBarController *tabBarController = [[UITabBarController alloc]init];
    tabBarController.viewControllers = array_controllers;
    
    self.window.rootViewController = tabBarController;
    
    [self.window makeKeyAndVisible];
    
    
    self.ycwNumber = [[Number alloc]init];
    
    return YES;
}


创建一个视图YcwViewController  添加一个button事件

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tabBarItem.title = @"first";
        self.tabBarItem.image = [UIImage imageNamed:@"111.png"];
    }
    return self;
}

- (IBAction)buttonAction:(id)sender {
    
    int i = [[delegate.ycwNumber valueForKey:@"badgeValue_number"] intValue];
    
    i = i+1;
    
    [delegate.ycwNumber setValue:[NSNumber numberWithInt:i] forKey:@"badgeValue_number"];
    
    
}

创建一个视图YcwThirdViewController  

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tabBarItem.title = @"third";
        self.tabBarItem.image = [UIImage imageNamed:@"333.png"];
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    delegate = [[UIApplication sharedApplication]delegate];
    [delegate.ycwNumber addObserver:self forKeyPath:@"badgeValue_number" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
    
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    
    
    if ([keyPath isEqualToString:@"badgeValue_number"]) {
        self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@",[delegate.ycwNumber valueForKey:@"badgeValue_number"]];

    }

    
    
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值