key-value-observing

Send Notification When a Property is Changed Using KVO

孤街浪徒 提交于 2019-12-21 07:26:14
问题 I had a property named myName in my class, like: @property (nonatomic, strong) NSString *myName; I need to send a notification when the myName property's value is changed. Now I'm doing something like: - (void)setMyName:(NSString *)name { _myName = name; [[NSNotificationCenter defaultCenter] postNotificationName:CHANGE_NOTIFICATION object:nil]; } I know there is something like Key-Value Observing in iOS. But I don't know how to implement it, I read the entire document, but couldn't get a good

KVO rocks. Now how do I use it asynchronously?

北慕城南 提交于 2019-12-21 05:22:26
问题 I am sold on KVO but if used in the obvious way it is synchronous. I would like to use it in a situation where I am firing off many KVO messages in rapid succession and it is causing my app to grind to a halt as the KVO messages are handled. Can someone suggest an approach - perhaps using NSOperation or NSThread - that will work here? My goal is to retain the decoupled, flexibility of KVO if possible. 回答1: Check out NSNotification. It's not quite the same thing, but you can fire off

Observing changes to a UIView's window and superview properties

爱⌒轻易说出口 提交于 2019-12-20 11:15:10
问题 I'm looking for a way to be notified when a generic UIView is added or removed from the visible view hierarchy. KVO looked like the perfect thing to use in this case, but observing changes to a view's window or superview properties doesn't do anything. Changes to properties like frame, or backgroundColor work as expected but changed to properties relating to the view hierarchy doesn't seem to ever call observeValueForKeyPath. I checked to see if UIView supports KVO on those properties by

How to use KVO for UserDefaults in Swift?

吃可爱长大的小学妹 提交于 2019-12-20 10:29:43
问题 I'm rewriting parts of an app, and found this code: fileprivate let defaults = UserDefaults.standard func storeValue(_ value: AnyObject, forKey key:String) { defaults.set(value, forKey: key) defaults.synchronize() NotificationCenter.default.post(name: Notification.Name(rawValue: "persistanceServiceValueChangedNotification"), object: key) } func getValueForKey(_ key:String, defaultValue:AnyObject? = nil) -> AnyObject? { return defaults.object(forKey: key) as AnyObject? ?? defaultValue } When

NSProxy and Key Value Observing

一世执手 提交于 2019-12-20 09:56:29
问题 NSProxy seems to work very well as stand-in objects for those that don't yet exist. For example. - (NSMethodSignature *)methodSignatureForSelector:(SEL)sel { return [self.target methodSignatureForSelector:sel]; } - (void)forwardInvocation:(NSInvocation *)invocation { [invocation invokeWithTarget:self.target]; } The above code will transparently pass any method invocation to the target that the proxy represents. However, it doesn't seem to handle KVO observations and notifications on the

Cocoa Key Value Bindings: What are the explanations of the various options for Controller Key?

允我心安 提交于 2019-12-20 09:24:14
问题 When I bind a control to an NSArrayController using Interface Builder, there are a variety of options under the "Controller Key" field in the bindings inspector. I understand what "arrangedObjects" is, and I semi-understand what "selection" is, but I'd love to see a really nice explanation of all the options and when to use each one. The list includes: selectionIndexes, selectionIndex, selectedObject, sortDescriptors, etc. I haven't been able to find a good explanation of these options. I'm

Cocoa Key Value Bindings: What are the explanations of the various options for Controller Key?

﹥>﹥吖頭↗ 提交于 2019-12-20 09:23:33
问题 When I bind a control to an NSArrayController using Interface Builder, there are a variety of options under the "Controller Key" field in the bindings inspector. I understand what "arrangedObjects" is, and I semi-understand what "selection" is, but I'd love to see a really nice explanation of all the options and when to use each one. The list includes: selectionIndexes, selectionIndex, selectedObject, sortDescriptors, etc. I haven't been able to find a good explanation of these options. I'm

uiwebview with header and footer

℡╲_俬逩灬. 提交于 2019-12-20 06:03:30
问题 I am trying to add header and footer (both of them as UIViews ) but for some reason my footer sticks to the bottom, I'm using the KVO method for watching my content size. I'm presenting here the method where I think the problem is: - (void)updateLayout { // Update the frame of the header view so that it scrolls with the webview content CGRect newHeaderFrame = self.headerView.frame; CGRect newFooterFrame = self.footerView.frame; newHeaderFrame.origin.y = -CGRectGetMinY([self.webView

MKMapView constantly monitor heading

♀尐吖头ヾ 提交于 2019-12-19 11:37:11
问题 I'm rendering some content in a layer that sits on top of my MKMapView . The whole thing works great with the exception of rotation. When a user rotates the map I need to be able to rotate what I'm rendering in my own layer. The standard answer I found is to use: NSLog(@"heading: %f", self.mapView.camera.heading"); The issue with this is that the content of the heading variable only updates when the pinch/rotate gesture is ending, not during the gesture. I need much more frequent updates.

NSSortDescriptor on transient attribute for NSFetchedResultsController

纵饮孤独 提交于 2019-12-19 05:49:14
问题 Ok, I initially wanted to make NSSortDescriptor of a request for NSFetchedResultsController to sort based on the property in my NSManagedObject subclass, but It obviously won't do it, because NSFetchedResultsController is limited to predicates and sort descriptors that work on the fetched entity and its relations, so I decided to create a transient attribute in my data model, synthesis the property for this attribute to ivar in my NSManagedObject subclass, and sort based on it. When running