Reputation: 1363
my dock menu always be added "Quit" and other 2 menu items automatically, how may I block / modify them?
updated:
really NO way to delete/block/redirect the "Quit" menu item. used Peter's recommendation at last like blow hope helpful to others
-(NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
if (needPassword)
{
[self checkPassword:self];
return NSTerminateCancel;
}
else
{
return NSTerminateNow;
}
}
-(void)checkPassword:(id)sender
{
if(passwordCorrect)
{
!needPassword;
[[NSApplication sharedApplication] terminate:self];
}
}
Upvotes: 3
Views: 1997
Reputation: 96333
Trying to intercept all possible ways the user might tell your application to quit is bound to fail. (Did you remember the Quit Apple Event?)
It'll be both easier and more effective to just implement the applicationShouldTerminate:
method in your application's delegate. Put up the password panel and return NSTerminateLater
. Then, when the user either enters the correct password or cancels, send the application a replyToApplicationShouldTerminate:
message.
Whichever Quit commands (menu items, etc.) you've already ripped out, put them back. Let the user invoke the normal Quit command in the normal way; that will trigger the aforementioned should-terminate procedure to determine whether the quit will actually happen.
Upvotes: 8
Reputation: 3389
1)Open the MainMenu.xib 2)Create your own dock menu 3)Right click on the File's Owner (NSApplication instance) 4)Connect the property "dockMenu" with your custom menu
If you want to do that because of learning purposes it's fine. However, when you want to sell this application you should reconsider this. Users expect your app to have a quit button in the dock menu.
Upvotes: 1