How to force-update Cocoa bindings? - objective-c

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")

Related

Why is the value of variable different when using CCLog and using print in debug mode?

The strengthbar in my game doesn't work well after playing at least once of the level. So I wrote a method using CCLog to log out some variables and call this method in update: method. And I also set a breakpoint, when this problem appears it gets into debug mode of Xcode, then I use print to check the variables. However it appears that the values are different between using CCLog and using print command. Here's a screenshot that explains everything.
Here is my screenshot:
Does it have something to do with Multithreads(which I know nothing about)? I've checked the declaration of each logged variables, to make sure they're not declared several times using the same name.
OK, problem is solved.
When I wrote some delegate I used "Strong" which causes some retain cycles. Now I changed them all to Weak, and also changed the deployment settings to iOS 5 (or above).
The reason that the values of the variable are different is because in CCLog, the value belongs to the new scene, however there're more than one scene at the same time receiving my touches because of the retain cycles. Nothing wrong about XCode or Cocos2d, or CCBReader :-p
Hope this answer would help others. And many thanks to #Cy-4AH.
Because it was changed after it was printed. In the breakpoint you are lookng at new value.

Showing past entries on UITextField

I am writing an app in which I have two UITextFields...
Starting Text
Destination Text
Now once I place values and hit the search or whatever function I want to call, I Want to reuse these values. The app should record and save these values as cache. And should show them when typing or upon a button click. Is that possible to show them just like Dictionary words show up or Which is more preferable tableView or PickerView? If there is any other please let me know.
Definitely use a UITableView. A UIPicherView is used modally most of the time and not for optional suggestions.
Table view is used in Safari, and a lots of other apps:
As for how to cache the data, you have lots of options. It also depends on how much data you expect.
One easy way would be to simply use NSArray. You can very easily write an NSArray to disk in a plist file and read it back when you need it.
Or you could use Core Data, if you expect lots of data and still want high performance. It will be a lot more difficult though to get used to that API if you've never tried it before. Basically you'll need a simple model with one entity called something like SearchEntry that has a single property text. Then you keep adding new instances to your managed object context and can easily filter the existing values.

create a new object

I want to create a new object so as to instantiate and use it several times;
For example, if I want to create an object that has a label and a button inside, how do I? I created a new NSObject but inside it has nothing, then how do I make everything from scratch since there was a viewDidLoad for example (obviously, since it has a view)?
thanks!
Your questions lead me to think that you're really just starting out. There's nothing wrong with that, but rather than trying to summarize several megabytes of documentation in a few paragraphs, I'm just going to point you to the iOS Starting Point. I think that you're just trying to create a container that can hold other UI components? If so, use a UIView for that. However, don't jump in and try to get something specific done without first reading through some of the Getting Started documents -- you'll just end up back here, and we'll just point you back to the docs. You might like the Your First iOS Application guide, as that lets you get your feet wet but explains things along the way.

Core data/NSPersistentDocument marking file "clean" every time any field finishes editing

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.

NSTableView, multiple cells and bindings

I'm trying to create a view that's similar to Motion's properties views.
Each of my property objects contains a definition of the kind of cell it wants to display as. But at the same time, I'd like to use bindings so that values are automatically updated as they can be changed elsewhere.
I've tried a few different approaches to the problem.
Multiple cells and dataCellForTableColumn: while this allows rendering to happen properly for all cell types, I lose bindings.
NSProxy: I've also tried using a proxy object that I thought would forward all methods to the selected cell type behind it, but again, bindings don't seem to work here.
Has anybody had any experience with this kind of problem before? Or is this one of the cases where bindings isn't going to cut it, and I'll need to do the heavy lifting myself?
Cheers!
I haven't actually tested it, but you shouldn't be losing bindings when you use dataCellForTableColumn:row: ? Can you describe in a bit more detail what you've tried with respect to bindings?
You should be able to set the column's value binding to the "value" property of your object. Then if you return the correct type of cell in dataCellForTableColumn:row: it should display correctly.
Edit: --- deleted gratuitous incorrect advice about cells vs views :)