NSUndoManager in Document based Application wrong behaviour - objective-c

In my document based app I call prepareWithInvocationTarget and registerUndoWithTarget for [self undoManager]. However, in Edit menu, Undo & Redo are still blank. I can execute undo manually by calling [[self undoManager] undo] and it works.
Just to make sure, I created a new project, called in it prepareWithInvocationTarget and registerUndoWithTarget. Magic! Undo/Redo elements in Edit menu could be clicked now.
What could be a possible reason for this? I tried checking connections in interface builder but they are the same as in the new project I created. Any idea how to fix it?

Ok, I've got it worked out.
The issue was that I had a document based project and in my document delegate I callsed [self undoManager]. I was supposed to call it on the main window instead. I've chcanged it to [mProjectWindow undoManager] (because mProjectWindow points to the main window of the document) and now eveyrthing works fine.

Related

NSMenuBar apps influencing (disabling) each others views (NSMenuItem's) ? BUG

I'm working on a NSMenuBar application and another NSMenuBar app seems to influence the display behaviour of my application!? More precisely: Adobe's Creative Cloud Application. When I click on that (creative cloud) Icon, the popup-view appears. Afterwards, when I click on my StatusBar Icon, my Menu appears, BUT, all the NSMenuItem's that should be Enabled are suddenly Disabled, and not interactive anymore.
Some observations:
When I log the enabled-state of the NSMenuItem, it logs correctly as being Enabled, but it is clearly not displaying as enabled (see screenshot), and clicking on it simply closes the Menu without performing the associated action. I do the logging with the following code:
-(void)menuWillOpen:(NSMenu *)menu
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC),
dispatch_get_main_queue(),
^{
NSLog(#"preferences menu-item is enabled: %hhd",[self.preferenceMenuItem isEnabled]);
});
}
The NSMenu I use is set to setAutoenablesItems:NO.
Also, Adobe's Creative Cloud uses a popup-view. And when it is opened, It clearly hovers over the screen-real-estate that my NSMenu is using.
When I try to reproduce this with other NSMenuBar applications (e.g. Apple's Wifi or Bluetooth) it all works. No problem. Then again, those applications don't use Popover-views, and they don't completely overlap the space of my app.
Please see these screenshots as illustration:
Before
Malicious Creative Cloud
After
What Am I missing here? Apps should not be influencing each other like this right? I'm slowly but surely losing my mind over this. Any help MUCH appreciated! Thnx!!
I have found the solution to my own problem. So I hope I will help anybody else struggling with this.
I was using my main xib file menu (with the status-bar) and referenced that menu to open when clicking on the statusbar icon. This caused the app to get in focus and to display two times the same menu (although not open at the same time), one under the statusbar-icon as intended, and one on the left side next to the apple symbol. Whenever the statusbar-menu was somehow getting deactivated, the left menu was still working, as if the focus was shifted to that one.
For me the solution was to decouple my referenced menu from the xib's menu, by dragging it outside the parent-menu. It's weird because somehow it's not visible anymore in InterfaceBuilder, but it's definitely still there. See screenshots:
BEFORE:
AFTER:

How to prevent displaying window on application startup osx

I am learning Objective-C and programming applications for OS X. I want to write myself a simple notifier, which should work this way:
connect to website and check, if there is new content (I have that covered)
display window with action buttons like "ignore", "visit website" (this is also easy)
I thought, that I will add my app to startup and if there are no changes on the website - just terminate it. But the window with action buttons will pop up before application checks, if there is a new content and window will flash quickly. How do I prevent that?
Is there a better way to create something like this? For example instead of running my application during startup - running a daemon, that will check the website every 24 hours and then display the window? How can I do that?
Recently, I read Start Developing Mac Apps Today, so I might be still missing something obvious.
Select NSWindow and just turn off Visible At Launch.
If you want to show the window, do it manually as follows.
- (void)applicationWillFinishLaunching:(NSNotification *)notification {
/* Do whatever necessary before the window appears */
[window setIsVisible:YES];
}
Personally, I never have the Visible-At-Launch switch on.

NSWorkspace selectFile:inFileViewerRootedAtPath: does not work the first time it is called

I have a sandboxed Cocoa app. I noticed that when I call [[NSWorkspace sharedWorkspace] selectFile:path inFileViewerRootedAtPath:path] no Finder window opens the first time is called. Finder.app is brought to the foreground (menu bar changes to Finder), but otherwise nothing happens.
When I bring my app back to the front and again have it select a file in Finder, the Finder window opens with the file selected as expected. I tried this with #"" as second argument, same thing.
When a Finder window is open already, everything works as expected.
Has anybody else seen this problem? Workaround is to immediately call selectFile:... twice, but that doesn't seem right.
Edit: not related to sandboxing. Same issue without sandbox.
I think activateFileViewerSelectingURLs is better. It correctly selects multiple files in the finder.

How can I open a file in another app using UIDocumentInteractionController?

I'm having trouble trying to open a file.
I download a document file somewhere, and I have a viewer to view the file on my iPhone. I want my app to open the file to the viewer.
Here's what I'm trying to do in -(IBAction)buttonPressed:
dc = [UIDocumentInteractionController interactionControllerWithURL:url];
if([dc presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES])
{
NSLog(#"menu is presented");
}
When I run the program and click trigger buttonPressed, the NSLog message is printed, and an action sheet appears with two options. One is evernote, the other one is viewer.
However, when I click either option in the action sheet popup, nothing happens.
What am I missing? Do I need to implement a delegate method?
Please help. Thanks!
i've got the answer.
i must retain dc..
fool mistake :(

Programmatically save causes document to think other app changes doc when re-opening file

This is pretty weird and I would very much appreciate all help =)
I have a document based app where it should be possible to perform some actions on the document file. To do so I'm saving the document every time the document actions are called. To do so I'm using the method:
saveDocumentWithDelegate:didSaveSelector:contextInfo:
The problem is, that when closing the document, reopening it from the recent files menu and then performing the action again I'm being presented with the dialog box saying that
This document’s file has been changed by another application since you opened or saved it.
However this is not the case when using the save menu item.
I've tried with different save methods:
saveToURL:ofType:forSaveOperation:error:
And even though this results in the behavior I'm looking for there is a side effect: The save menu item becomes deactivated after performing the action.
So my question is: How should I correctly perform save operations programmatically? I've looked through the docs but I haven't seen anything which looks like an obvious solution.
All help is appreciated. Thanks
I found solution for this problem in my case.
Problem was in options in overriding of configurePersistentStoreCoordinatorForURL:ofType:modelConfiguration:storeOptions:error: method of NSPersistentDocument. Problem was solved when I delete string
[options setObject:[NSNumber numberWithBool:YES] forKey:NSSQLiteManualVacuumOption]
I used vacuum option for some reason. Don't know how but it caused problem with saving. When I refused this option the problem has disappeared.
To get it to work as you want, you can probably fix it by using the saveToURL:... method and then call -[NSWindow setDocumentEdited:] or -[NSDocument updateChangeCount:] which will stop the menu item from being disabled.
However, I'm not sure your overall approach is right. If you have a save menu item, I don't think you should be saving automatically unless you're using Lion’s auto-save feature (recommended) in which case you would call different methods.
And even if you did decide to stick with the auto-saving as you have it now, why wouldn’t you want the menu item to be disabled? It’s disabled because there are no changes to save.
Why are you auto-saving the file anyway?