即时改变badgeValue的值(使用KVO)
来源:互联网 发布:软件学徒招聘骗局 编辑:程序博客网 时间:2024/05/20 10:21
首先先创建一个类
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"]];
}
}
- 即时改变badgeValue的值(使用KVO)
- 改变badgeValue
- 改变badgeValue
- badgeValue的设置和使用
- 详细的KVO总结,包括基本改变,使用案例
- swift中KVO简单使用,监听属性值改变
- KVO改变textfield的placeholderLabel的属性
- ios UISegmentedControl KVO 检测selectedSegmentIndex的改变
- KVO(key value observing)的使用
- KVO (key-value observing)的使用
- input propertychange事件,当input的value值发生改变时触发(文本框的值即时搜索)
- KVO的使用
- KVO的使用
- KVO的使用
- KVO的简单使用
- KVO的使用实例
- KVO的使用
- 关于KVO的使用
- uva 1513 - Movie collection(树状数组)
- POJ——Mobile phones
- Http-代理
- 中国十大特色购物网站
- [算法导论] 递归式求解的三种方法
- 即时改变badgeValue的值(使用KVO)
- eclipse字体颜色的设置方法
- 【POJ】1797 Heavy Transportation 二分+最短路
- iOS程序发布测试生成AD_HOC时需要的证书
- git 的使用(3)-基础命令
- Oracle Database 学习
- Android Service与Activity之间通信的几种方式
- Nginx负载均衡配置实例详解
- 龙组第5周总结