This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Observing a Change to ANY Class Property in Objective-C
I have a object currentUser which contains ints and const char*s and I am trying to implement a logout system. When the logout button is pressed, I want to be able to check if I have made any changes to the object and if so, I want to prompt the user to save before logging out.
In order to do so I think I need to be able to observe the changes, if any, that are made to the object. I considered KVO but I don't know how to observe the entire variable as a key path. Does anybody have any ideas on what I should do? Also if you could post some code as well that would help!
Not sure if this fits your needs but would posting a notification to NSNotificationCenter work?
Related
Is there a symbolic breakpoint or something that will trap the following warnings, so that the erroneous code can be more easily found?
Attempt to present <> on <> while a presentation or dismiss is in progress.
Trying to dismiss the presentation controller while transitioning already. (<>)
I don't have a specific problem to solve, just looking for an answer to this question, which could just be "no".
When these errors occur, how do you find out the present/dismiss call causing the problem and/or the present/dismiss that is in progress?
Maybe a little more context would help us answer your question ?
Are you using UIViewControllerAnimatedTransitioning ?
When are you having this issue happening ?
If you want to debug, you can use UINavigationControllerDelegate method for your convenience :
- navigationController:willShowViewController:animated: or
navigationController:animationControllerForOperation:fromViewController:toViewController:
If you're not already using it for your animation.
The best I've found so far is to run with Allocations instrumentation and "Record reference counts".
Then you can find the specific instances referenced in the logs by their address.
This will have recorded the stack traces when that object was created, presented, etc, as they all involve changes in reference count.
I'm using Cocoa bindings (as in Objective-C on the Mac) to display a relative date value using a value transformer. That is, my NSValueTransformer subclass converts an NSDate instance to NSString to display relative dates like "3 seconds ago", "2 minutes ago", etc.
As you can see, these displayed values gets outdated as time progresses and thus will need to be refreshed somehow. I know I'll need to use a timer and then force the bindings to update so that the value transformer gets re-executed and display the correct relative date.
But the question is, how do I make these bindings to refresh their values?
If you are using bindings, then the GUI should update as long as you:
are updating the values on the main thread (so the bindings can be updated at GUI time)
are using the setter to update the value
So, if you're you've got the value bound to an object's foo.zot property, you need to make sure to call [foo setZot: #"new value"] on the main thread (or set the property using foo.zot=#"new value").
I realize that this question was asked long long ago, and also that forcing a Cocoa-Binding update may not be the right solution for this particular case, but as I came across the question while looking for a way to do this, I think it's worth it having the answer posted here.
You can force an update by making the bound-to object "think" that a property had changed, which will impel it to notify any observer of that property. This is done by calling both willChangeValue and didChangeValue consecutively and in this order, as in the following example:
boundToObject.willChangeValue(forKey: "boundToProperty")
boundToObject.didChangeValue(forKey: "boundToProperty")
I have a cocoa app that allows the user to enter a query. I'm using an NSWebView with a TextArea HTML object. The problem is, as soon as I type anything into the textarea, my document gets marked as updated. Does anyone know of a way to prevent this?
I've verified that using a NSTextField does not reproduce this behaviour, but I specifically want to go with the HTML/TextArea for styling.
So basically: Can I make it so an NSDocument does not get marked as edited unless I manually call:
[document updateChangeCount: NSChangeDone];
This post on the Apple mailing list seems to match your problem exactly.
The solution suggested is to set a custom undo manager to the webview (sounds like hard work), however a quick-and-dirty hack looks to me like subclassing updateChangeCount and perverting things to your way of thinking.
How would you solve this? When the app starts, four objects of a class are created. These objects have names you know because you named them yourself. From a viewController you can access these objects and call a method (which they all got) which creates a UILocalNotification. (So in the end you've got four notifications running.)
Two questions:
How do you name the notifications (differently)? As far as I know is it not possible to access the object name to use the string as name when creating the notification? (Which would be the best solution?)
When the notifications are fired, how do you access/cancel them from another viewController when you don't know the names?
Thank you!
Set tags for all objects, and set same tags for notifications, they generate.
I'm working with a document-based core-data OS X application. The problem I'm having is that whenever I edit any field on the document, after I press tab or click to something else (i.e. I finish editing/change focus), the document is marked as clean and undo is reset. When I try to save the file, however, the resulting document opens without the data I entered. What might be the problem, or, any pointers on where to look to fix this? Here's some stuff I know and things I've already tried:
I know it's not somehow saving because it never stops at the breakpoint in my overridden writeSafelyToURL:(NSURL *)inAbsoluteURL ofType:(NSString *)inTypeName forSaveOperation:(NSSaveOperationType)inSaveOperation error:(NSError **)outError and it also never sends an NSManagedObjectContextDidSaveNotification.
The documents are packaged in an NSFileWrapper directory with the core data store inside (and also some other files). I access the entities through an NSObjectController and a couple NSArrayControllers. It happens with both core data properties and manually registered changes in the rest of the file wrapper.
Update: At the suggestion of Martin, I tried NSUndoManager's notifications, and all I can seem to glean from it is that more than one undo manager is in play. If I add an observer for NSUndoManager, it won't post if I specify an object, and then if I don't, the notification object is not equal to [self undoManager]. I added updateChangeCount to my category on NSPersistentDocument, and it never gets called. setDocumentEdited basically confirmed that something about losing first responder is passing NO into that method. What could be causing this, and how can I fix it?
You could break on the method setDocumentEdited: of NSWindow to see which operation updates the change status.
In addition updateChangeCount: of NSDocument might be a place to take a look at.
NSUndoManager also posts several Notifications which can give additional hints what to look at.
The answer is actually pretty silly considering how long this stumped me. I was working on some objects on load, and I accidentally set [[self undoManager] disableUndoRegistration] at both the points where I should disable and enable. It was a little more than that, though. A related element in Interface Builder needed to be checked Prepares Content. When I had done both those things, the problem vanished.