观察变量值修改的位置
2015-10-20 13:54
525 查看
使用观察者模式观察变量值修改的位置
#import "ViewController.h" @interface ViewController () @property (nonatomic,strong) NSString *testString; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //添加观察者,自己观察自己的 testString [self addObserver:self forKeyPath:@"testString" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)stringChange1:(UIButton *)sender { self.testString = @"1"; } - (IBAction)stringChange2:(UIButton *)sender { self.testString = @"2"; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { //NSLog(@"%s:%d obj = %@",__func__,__LINE__,object); NSLog(@"%@",self.testString);//在这一句上打一个断点。 } @end
在 storyboard 中添加两个按钮,并分别添加方法 stringChange1 和 stringChange2,按下按钮时会断到断点位置,这时看 Xcode——左侧菜单栏——Show the Debug navigator,可以看到是从哪一个方法运行到了这里,从而确定 self.testString 是的值是在哪里被修改。
相关文章推荐
- 硬币问题(贪心)
- GRUB- "No suitable mode found"
- handsontable-chosen-editor
- JS日期时间类型(Y-m-d H:i:s)与时间戳互转
- Request的getParameter和getAttribute方法的区别
- 如何修改MFC的图标
- MDK Executing a function or an interrupt handler in RAM
- Window窗口布局 --- DecorView浅析
- 你应该知道的 RPC 原理
- 网址
- 接口类与抽象类的区别
- nmon的使用
- zookeeper原理(转)
- 简单的分析网易热这款产品
- Exchange 2013如何自建管理员角色
- 在ListView下面添加一个相对布局的小Dome-布局优化
- Hibernate 缓存机制
- 主线程在所有子线程结束后 在继续执行
- Toolbar中menu菜单文字颜色的修改
- 将已有的Android从Eclipse手动迁移到Android Studio(不用gradle)