When I use the red ‘X’ button to close the PasswordGenerator application, the app remains in the dock, as it should. However, I cannot click the PasswordGenerator app in the dock and have it restore to the screen like other apps do. This happens both when running the app from Xcode 7.2.1 and when I launch the app from Launchpad.
Shouldn’t clicking the PasswordGenerator app in the dock restore the app to the screen? If not, how do I restore the app to the screen?
Thank you for your reply. I read several articles on NSApplicationDelegate, but understood almost nothing about it. Getting used to Apple programming has been a real challenge.
One snippet of code I copy and pasted from the internet that was supposed to work is this:
However I got an error message “Expected declaration” which I don’t why I am getting it nor how to fix it. Here is all the code in the AppDelegate.swift file:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var mainWindowController: MainWindowController?
func applicationDidFinishLaunching(aNotification: NSNotification) {
//create a window controller with an xib file with the same name
let mainWindowController = MainWindowController(windowNibName: "MainWindowController")
//put the window of the window controller on the screen
mainWindowController.showWindow(self)
//set the property to point to the window controller
self.mainWindowController = mainWindowController
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
//“Expected declaration” error occurs on the following line of code
- (BOOL) applicationShouldOpenUntitledFile:(NSApplication *)sender
{
[window makeKeyAndOrderFront:self];
return NO;
}
}
It takes time to get used to Apple’s documentation for reference material, mainly because it is mostly poorly written. However, you can reduce the pain by reading tutorials and programming guides.
Now for the problem you are experiencing.
[quote]- (BOOL) applicationShouldOpenUntitledFile:(NSApplication *)sender
{
[window makeKeyAndOrderFront:self];
return NO;
}
[/quote]
That looks like the correct ApplicationDelegate method, but it is written in Objective-C; you need to write it in Swift.
Thank you for your reply. Learning Apple programming has been a real struggle for me. I have no clue when I am using Objective-C programming vs Swift programming vs Cocoa features. Plus I am told that Swift is a work-in-progress, so I am never sure if the error messages I receive are a result of an actual error in the code or the error is a result of the evolving Swift language. I am using these four books as guides:
Cocoa Programming for OS X: The Big Nerd Ranch Guide
Objective-C Programming: The Big Nerd Ranch Guide
Learn to Code in Swift
Mastering Xcode 7 & Swift, iOS App Development for Non-Programmers
but it doesn’t like “window”. It reports an error “Use of unresolved identifier ‘window’”. I looked in all the files of my program and I don’t see any identifier for the window. The Interface Builder has something they call Window (with a capital ‘W’) that I thought might be the name of the window, but that produced the same “Use of unresolved identifier ‘window’” error. So am I supposed to make a ‘window’ variable or is the name of my window hidden somewhere in the code? If I need to make a new ‘window’ variable, what is the type?
Out of curiosity, why doesn’t Apple include the ability to restore an application from the Dock part of the standard window behavior just like they do for the ‘Quit’, ‘Minimize’ and ‘Maximize’ buttons on the form?
When I write code for Microsoft Windows, I don’t need to write code to restore the window. The code is already included in the standard window framework.
When creating a new project, choose a language - Objective-C or Swift;
3.Use Cocoa as a framework (component library); and
If you feel (rightly) that Swift is a work-in-progress, then stick with Objective-C for now. Don’t just dive into Swift because everybody else is doing it. Don’t volunteer to be a crash-test dummy (you don’t get paid for it.) Once you have become comfortable with Objective-C, you can start the transition to Swift.
Thank you again for all of your help. I added your code:
@IBOutlet weak var window: NSWindow!
and successfully connected it to the Window object in IB.
When I first ran the program, I got a successful build, but I got an immediate error at:
I stopped and restarted Xcode and the program runs fine if I use the yellow ‘-’ button to minimize the window. However, if I use the red ‘X’ button to close the window, I get the following error when I try to restore the window:
“fatal error: unexpectedly found nil while unwrapping an Optional value”
Virtually every other program on my Mac restores successfully after using the red ‘X’ to close the window. Why is this program different and can I make the program restore successfully after using the red ‘X’ to close the window?
[quote]“fatal error: unexpectedly found nil while unwrapping an Optional value”
[/quote]
That error is saying that the window outlet is floating; that is, it is not connected to the window object in the xib file.