cocoa how to block the “Quit” menu item on dock menu

爷,独闯天下 提交于 2019-12-08 07:37:36

问题


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];
    }
}

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/8424346/cocoa-how-to-block-the-quit-menu-item-on-dock-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!