Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Quitting app when hitting close button

Quitting app when hitting close button
Thread Tools
OptimusG4
Mac Elite
Join Date: Feb 2003
Location: columbus, oh
Status: Offline
Reply With Quote
Aug 29, 2003, 12:46 AM
 
Does anyone know how to get an app to quit when you hit the close button? I'm working on a simple app and when you hit the close button, I want it to quit instead. Thanks!
"Another classic science-fiction show cancelled before its time" ~ Bender

15.2" PowerBook 1.25GHz, 80GB HD, 768MB RAM, SuperDrive
     
proton
Senior User
Join Date: Nov 2000
Status: Offline
Reply With Quote
Aug 29, 2003, 01:41 AM
 
Set a delegate on your application's NSApplication, implementing

- (BOOL)applicationShouldTerminateAfterLastWindowClo sed:(NSApplication *)theApplication

The documentation for NSApplication is available from the Apple Developer Site

- proton

[Edit: disable smileys]
     
Devin Lane
Forum Regular
Join Date: Aug 2003
Status: Offline
Reply With Quote
Aug 29, 2003, 11:22 AM
 
There is also another way:

Make a class and set it to the window's delegate. Then implement this menthod:

- (void)windowWillClose:(NSNotification *)aNotification {
[NSApp terminate];
}

This is probably not as nice as proton's way, but it should work.
-- Devin Lane, Cocoa Programmer
     
Jan Van Boghout
Registered User
Join Date: Sep 2001
Status: Offline
Reply With Quote
Aug 29, 2003, 02:23 PM
 
Originally posted by Devin Lane:
There is also another way:

Make a class and set it to the window's delegate. Then implement this menthod:

- (void)windowWillCloseNSNotification *)aNotification {
[NSApp terminate];
}

This is probably not as nice as proton's way, but it should work.
This is the preferred way if you don't want preferences windows etc preventing the quit, but it depends on what he wants to use it for
     
OptimusG4  (op)
Mac Elite
Join Date: Feb 2003
Location: columbus, oh
Status: Offline
Reply With Quote
Aug 29, 2003, 02:43 PM
 
Hey thanks all I'll work on implementing it this weekend since I have a funeral tomorrow.
"Another classic science-fiction show cancelled before its time" ~ Bender

15.2" PowerBook 1.25GHz, 80GB HD, 768MB RAM, SuperDrive
     
ambush
Banned
Join Date: Apr 2002
Location: -
Status: Offline
Reply With Quote
Aug 29, 2003, 05:15 PM
 
isn't it [NSApp terminate:self]; ?
     
nickm
Mac Enthusiast
Join Date: Dec 2001
Status: Offline
Reply With Quote
Aug 30, 2003, 09:12 AM
 
Why do this? As a user, when I click the close button, I expect the window to close, not have the app quit on me. If I had wanted to quit the app, then I would have quit it.

I think having apps quit when their last window is gone is a bad idea.
     
Diggory Laycock
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Aug 30, 2003, 10:36 AM
 
It can work well when the app only has one window.
     
Uncle Skeleton
Addicted to MacNN
Join Date: Nov 2002
Location: Rockville, MD
Status: Offline
Reply With Quote
Aug 30, 2003, 11:41 AM
 
Originally posted by nickm:
Why do this? As a user, when I click the close button, I expect the window to close, not have the app quit on me. If I had wanted to quit the app, then I would have quit it.

I think having apps quit when their last window is gone is a bad idea.
worse is when you can close the main window and then there's no way to get it back except by manually quitting and restarting...
     
chabig
Addicted to MacNN
Join Date: Jun 1999
Location: Las Vegas, NV, USA
Status: Offline
Reply With Quote
Aug 30, 2003, 12:36 PM
 
I agree. Just because I want the window closed and out of the way does not mean I want to quit the application. Just close the window and let me open it from the Window menu.

The red window widget means "close". It does not mean "quit".

Even if the app has just one window doesn't mean I want to quit. There are a variety of reasons people want to close windows:

1. To see something underneath.
2. To put it aside temporarily.
3. To reduce window clutter.

There is only one reason to quit: because I'm done with that application.

I don't see the overlap that makes some developers think the only reason I want to eliminate the window is because I'm done.

Chris
     
Devin Lane
Forum Regular
Join Date: Aug 2003
Status: Offline
Reply With Quote
Aug 30, 2003, 12:51 PM
 
Originally posted by ambush:
isn't it [NSApp terminate:self]; ?
Yes, you're correct. Sorry. Actually, though, you probably don't need to pass self in most cases, nil should do fine.
-- Devin Lane, Cocoa Programmer
     
OptimusG4  (op)
Mac Elite
Join Date: Feb 2003
Location: columbus, oh
Status: Offline
Reply With Quote
Aug 30, 2003, 02:24 PM
 
Originally posted by chabig:
I agree. Just because I want the window closed and out of the way does not mean I want to quit the application. Just close the window and let me open it from the Window menu.

The red window widget means "close". It does not mean "quit".

Even if the app has just one window doesn't mean I want to quit. There are a variety of reasons people want to close windows:

1. To see something underneath.
2. To put it aside temporarily.
3. To reduce window clutter.

There is only one reason to quit: because I'm done with that application.

I don't see the overlap that makes some developers think the only reason I want to eliminate the window is because I'm done.

Chris
For one, its a one window app for simple stuff that my mom has requested, so I'm building it for her. Two, she switched from a PC and to her, closing a window means exit the program, and before you know it, she has 12 apps open on an iBook and it slows to a crawl.

Oh and look at Calculator, it does exactly what I was asking and no one's b*tched yet about it.
"Another classic science-fiction show cancelled before its time" ~ Bender

15.2" PowerBook 1.25GHz, 80GB HD, 768MB RAM, SuperDrive
     
DaveGee
Mac Enthusiast
Join Date: Mar 2001
Status: Offline
Reply With Quote
Sep 2, 2003, 07:48 AM
 
Originally posted by OptimusG4:
Oh and look at Calculator, it does exactly what I was asking and no one's b*tched yet about it.
Give em time... give em time...

I too am developing my first cocoa app (first obj-c or any c for that matter app too) and I too would like it to 'quit on close' - it's a single window app and it just 'feels' like the type of app that once you close the window you are done with it and thus it should quit.

Anyway I'm SUCH a newbie when it comes to this stuff it's very frustrating at times and yet gratifying at others...

I tried adding that code (found it elsewhere) but it didn't seem to do anything - must have done something wrong... see what I mean by frustrating...

Dave
     
ambush
Banned
Join Date: Apr 2002
Location: -
Status: Offline
Reply With Quote
Sep 2, 2003, 11:12 AM
 
Originally posted by Devin Lane:
Yes, you're correct. Sorry. Actually, though, you probably don't need to pass self in most cases, nil should do fine.
self is always safer..
     
parsec
Forum Regular
Join Date: Mar 2001
Location: canada
Status: Offline
Reply With Quote
Sep 4, 2003, 08:09 PM
 
Originally posted by DaveGee:
Give em time... give em time...

I too am developing my first cocoa app (first obj-c or any c for that matter app too) and I too would like it to 'quit on close' - it's a single window app and it just 'feels' like the type of app that once you close the window you are done with it and thus it should quit.

Anyway I'm SUCH a newbie when it comes to this stuff it's very frustrating at times and yet gratifying at others...

I tried adding that code (found it elsewhere) but it didn't seem to do anything - must have done something wrong... see what I mean by frustrating...

Dave
Did you remember to make the class NSApplication's delegate?
     
DaveGee
Mac Enthusiast
Join Date: Mar 2001
Status: Offline
Reply With Quote
Sep 5, 2003, 08:45 PM
 
Originally posted by parsec:
Did you remember to make the class NSApplication's delegate?
Thanks parsec!

Yea I finally remembered errr learned how to do that... All part of the 'learn as you need to' method of learning cocoa...

Dave
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 08:34 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,