- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是 iPhone 新手。我正在尝试实现 KVO 机制。
我有什么?
两个TabController有两个UIViewController,FirstViewController有一个按钮,SecondViewController 有一个 UITextView
我想要什么?
当在 firstViewController 中按下按钮时,它会更新成员变量,这应该被 secondViewController 观察到,并且它应该附加到 UITextView。
我做了什么?
FirstViewController.h
@interface FirstViewController : UIViewController
{
IBOutlet UIButton *submitButton;
}
-(IBAction)submitButtonPressed;
@property (retain) NSString* logString;
@end
FirstViewController.m
-(IBAction)submitButtonPressed
{
NSLog (@" Submit Button PRessed ");
self.logString = @"... BUtton PRessed and passing this information ";
}
SecondViewController.h
@interface SecondViewController : UIViewController
{
IBOutlet UITextView *logView;
}
@property (nonatomic, strong) UITextView *logView;
@end
SecondViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
......
NSArray *vControllers = [self.tabBarController viewControllers];
FirstViewController *fvc = [vControllers objectAtIndex:0];
[fvc addObserver:self forKeyPath:@"logString" options:NSKeyValueObservingOptionNew context:NULL];
return self;
}
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog (@".... OBSERVE VALUE FOR KEY PATH...");
}
我期望得到什么输出?
每次我按下 FirstViewController 中的按钮时,都应打印字符串“.... OBSERVE VALUE FOR KEY PATH...”。
我得到了什么?
无输出。
我做错了什么?请帮助我
最佳答案
将您的“成员变量”放入一个单独的类文件中……即模型/ View / Controller 。创建一个保存数据的单例模型对象,然后您可以从任何 View Controller 对该模型对象进行 KVO。
这是粗略的伪代码:
@interface MyDataModel: NSObject
{
NSString *logString;
}
@property (nonatomic,retain) NSString *logString;
@end
@implementation MyDataModel
@synthesize logString;
+ (MyDataModel *) modelObject
{
static MyDataModel *instance = nil;
static dispatch_once_t once;
dispatch_once(&once, ^{
if (instance == nil)
{
instance = [[self alloc] init];
}
});
return (instance);
}
@end
在你的 VC1 中
MyDataModel *model = [MyDataModel modelObject];
[model setLogString:@"test"];
在你的 VC2 中
[model addObserver:self forKeyPath:@"logString" options:0 context:NULL];
一种更复杂的方法是使用核心数据作为持久存储并充当您的数据模型。
关于objective-c - UITABViewController中两个UIViewControllers之间的KVO机制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11883818/
当我的应用程序关闭时,主 Controller 类将其自身作为观察者从模型中删除,然后释放模型。像这样: - (void)dealloc { [theModel removeObserver:
我正在努力加深对这些机制的理解。我有一个启用触摸的 UIView,可以更新它自己的 center 属性。 我的理解是frame属性是center和bounds的合成函数。假设这是真的,我将一个观察者放
我正在尝试使用 KVO 来观察在我的页面 View Controller 的子内容 View Controller 的 ScrollView 中使用拖动时的更新变化,但是当应用程序启动时,它崩溃了说:
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我尝试使用 KVO 观察 UIView 子类上的属性,以便通过调用 drawRect: 触发绘制。在我的 initWithFrame: 中,我有这个: ... self.observedKeysTha
我以前使用过 NSNotifications,但这是我第一次尝试在 Cocoa Touch 中使用 KVO。 我的 UITableView Controller 在各种数据源之间切换,因此我将它们封装
我遇到了 KVO 问题,但我不知道它是什么。 我有一个可用于编辑或创建记录的屏幕。该记录由一个模型对象表示,该对象包含许多 NSString 类型的变量。 该字符串的 Controller 有一个实例
这个问题已经有答案了: In Cocoa KVO, why doesn't a change on a NSMutableArray proxy notify observers? (1 个回答) 已
当我想观察保留属性的变化时,我经常遇到这样的情况: @interface AnObserver {…} @property(retain) Foo *foo; 现在,当我想要设置观察时,我需要编写自己
我定义了两个具有相应 ivars 的属性并合成了它们: @property (nonatomic,copy) NSString* title; @property (nonatomic,copy) N
我试图掌握绑定(bind),并在 xcode 中设置了一个带有 CPTextView 的 CPWindow,并将窗口和 textview 绑定(bind)到它们各自的导出。 我有点卡住了,但是试图让
一定有一些我对 KVO 没有了解的地方,我试图通过滚动 UIScrollView 来滚动 UITableView,偏移量转换是正确的,但是 UITableView 滚动发生在 UIScrollView
我正在学习 MVVM,所以我在属性上添加了观察者 init(model: RegisterUserModel = RegisterUserModel()) { self.registerMo
我的 iPhone 应用程序核心数据属性的自定义访问器方法有问题。我想做的不仅仅是取出原始类型并返回它。我有一个可能的 activityTypes 的 NSSet,如果对象没有 primitiveTy
事情是这样的:我有一个单例监视设备的事件存储的变化。我有一个名为 events 的属性,我已将其包装在 eventsSignal RACSignal 中。 _eventsSignal = [RACAb
我得到的 iOS 应用 实体很少,它们由关系链接。举一个简单的例子,假设我们有这样的关系(一对多): company > person 我正在使用 xcode 4.6 和用于模型生成的核心数据建模工具
我在 iOS 应用程序中遇到 KVO 问题。我似乎无法正确处理。我正在尝试做什么: 我有一个 MenuViewController(可在左侧显示,如 facebook 应用程序)观察(并显示)全局值。
我试图通过以下代码从其他对象观察 UIViewController 的 KVO 的界面方向: [((UIViewController *)self.delegate) addObserver:self
我正在尝试使用 KVO 在 UILabel 和对象数据之间创建一个小型绑定(bind)系统。如果我的 UI 发生变化,我的数据也必须发生变化,如果我的数据发生变化,我的 UI 应该刷新以显示新值。 我
如果我在我的 iOS 应用程序上设置了本地通知,我将更新一个按钮。我想用 KVO 做到这一点,因为可以通过多种方式设置通知,所以我将无法轻松跟踪本地通知何时发生更改,否则。 我是这样设置的:
我是一名优秀的程序员,十分优秀!