Im trying to allow the user to decide whether to quit the application or not and Ive been trying to implement it using this:
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
int answer = NSRunAlertPanel(#"Quit", #"Are you sure?", #"Quit", #"Cancel", nil);
if (answer == NSAlertDefaultReturn) { return NSTerminateNow;
} else { return NSTerminateCancel;
}
}
I have placed this in my AppDelegate.m and linked the delegate to my main window in interface builder. When i debug and run the application in Xcode, and press the close button, the app window closes but the alert panel does not pop up..
Am i doing something wrong? thank you for your help!! I am new here and I hope someone can help me with this.. Thank you so much!
By default closing a window will not quit the application, thus not triggering your alert panel. To make the application quit and show your alert when the user closes the window just add this to the delegate:
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)aApplication {
return YES;
}
Related
I have a NSWindowController which closes itself by pressing the 'x' key and works great
as you see, very clear my Window, this is the code
-(void)keyDown:(NSEvent *)theEvent{
//If the key is X just closes the window
if ([theEvent.characters.uppercaseString isEqualToString:#"X"]) {
[self.window performClose:self];
}
}
but when I remove the TitleBar or the close control it just stops working...
I want it this way because its required to have my windows without buttons, just to close or do task by shortcuts and commands, in this case the X button to close, how to perform this in a NSWindowController without the close control or titlebar
thanks for the support
with this:
-(void)keyDown:(NSEvent *)theEvent{
//If the key is X just closes the window
if ([theEvent.characters.uppercaseString isEqualToString:#"X"]) {
[self close];
}
}
I'm a little bit new to Objective-c with xCode, and I would like to know something. Is there a way to terminate an application when the red circle in the left of the window is clicked? Like on the calculator.
Yes you can do with Mac OSX applications.
You need to implement this method in your AppDelegate class
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender{
return YES;
}
If you want terminate the app when closing the window. Please implement the following appdelegate method.
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{
return YES;
}
If you want do not terminate your app, just set the return "NO".
I hope that your problem will be resolved with this solution.
I've been making an app that I need help with. After I do what I want the app to do, it goes to a new page. when I press the go back button, it is still in the same state as when I left. I want the first view to completely reset when I hit the button. Any suggestions as to how to do this? Any help is appreciated!
Here is code for what the button currently does:
- (IBAction)retry:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
}
Implement viewWillAppear on your initially displayed viewController as following:
- (void)viewWillAppear:(BOOL)animated
{
//
//put your code to reset this viewController into its initial state here
//
[self resetAllStatesBackToDefault];
}
As an example, suppose you got a UITextField on that first viewController that is named textField;
- (void)resetAllStatesBackToDefault
{
self.textField.text = #"";
}
What you need is the -(void)viewDidAppear:(BOOL)animated;
I'm trying to display a modal dialog on top of my app but it's blocking my main app window when it closes. Here's my code:
TutorialWindowController* pTutorialController = [[TutorialWindowController alloc] initWithWindowNibName:#"TutorialWindow"];
NSWindow* pTutorialWindow = [pTutorialController window];
DDLogInfo(#"Tutorial window opening...");
[NSApp runModalForWindow: pTutorialWindow];
DDLogInfo(#"Tutorial window closed!"); // CODE NEVER GETS HERE
[NSApp endSheet: pTutorialWindow];
[pTutorialWindow orderOut: self];
In the modal dialog, my Close button runs this:
- (IBAction)closeButtonPressed:(id)sender {
[NSApp stopModal];
}
The modal dialog displays fine. However, when I click the Close button, the dialog disappears and my app's main window isn't responsive. I hear the bonk every time I try clicking. I'm pretty sure this is because the code never continues after runModalForWindow. Same thing happens if I close the modal dialo using the red X.
What am I doing wrong?
After ordering out the tutorial window, try doing a
[window makeKeyAndOrderFront:self];
on your main window.
You should call [pTutorialWindow orderOut:nil] first.
Not sure about the closeButtonPressed handler. But try adding to the delegate:
- (void) windowWillClose:(NSNotification *)notification
{
// ...
// In there, you should verify that you are calling:
[NSApp stopModal]
}
Adding the stopModal call solved the issue for me.
Verify that the Window delegate in the Interface Editor's Connection Inspector is connected to the File's Owner.
I had several modal dialogues working correctly except for one, and the missing connection was the only difference. Making the connection fixed the problem.
how could i make my Xcode application terminate completely when the user presses the X button.
Could you also provide some detail cause i'm sort of a beginner.
EDIT: Sorry for my vagueness, This is a desktop application.
Use - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender method of AppDelegate. Sample:
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{
return YES;
}
Send the following message: [NSApp terminate:nil].