Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 24, 2010, 04:51 PM   #1
Thom_Edwards
macrumors regular
 
Join Date: Apr 2003
MTCoreData_AppDelegate.m and a Cancel button

I've diving in to Cocoa, coming from a php and ASP.Net (with C#) background. I'm making good headway, but I've hit a bump and need some help from you guys.

First off, here is the code that is confusing me. Specifically, it's the last two ifs, which I detail later.
Code:
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {

    if (!managedObjectContext) return NSTerminateNow;

    if (![managedObjectContext commitEditing]) {
        NSLog(@"%@:%s unable to commit editing to terminate", [self class], _cmd);
        return NSTerminateCancel;
    }

    if (![managedObjectContext hasChanges]) return NSTerminateNow;

    NSError *error = nil;
    if (![managedObjectContext save:&error]) {
    
        // This error handling simply presents error information in a panel with an 
        // "Ok" button, which does not include any attempt at error recovery (meaning, 
        // attempting to fix the error.)  As a result, this implementation will 
        // present the information to the user and then follow up with a panel asking 
        // if the user wishes to "Quit Anyway", without saving the changes.

        // Typically, this process should be altered to include application-specific 
        // recovery steps.  
                
        BOOL result = [sender presentError:error];
        if (result) return NSTerminateCancel;

        NSString *question = NSLocalizedString(@"Could not save changes while quitting.  Quit anyway?", @"Quit without saves error question message");
        NSString *info = NSLocalizedString(@"Quitting now will lose any changes you have made since the last successful save", @"Quit without saves error question info");
        NSString *quitButton = NSLocalizedString(@"Quit anyway", @"Quit anyway button title");
        NSString *cancelButton = NSLocalizedString(@"Cancel", @"Cancel button title");
        NSAlert *alert = [[NSAlert alloc] init];
        [alert setMessageText:question];
        [alert setInformativeText:info];
        [alert addButtonWithTitle:quitButton];
        [alert addButtonWithTitle:cancelButton];

        NSInteger answer = [alert runModal];
        [alert release];
        alert = nil;
        
        if (answer == NSAlertAlternateReturn) return NSTerminateCancel; //doesn't work
	if (answer == 1001) return NSTerminateCancel; //works		
    }

    return NSTerminateNow;
}
I started a Core Data Cocoa application in version 3.2.4 in Snow Leopard, set up my data model, laid out the interface, and set up the bindings. In the data model I have an entity with a property (namely isbn, this is a book catalog app) set with Optional off and no Default Value. Note that I have not changed one line of code--I'm just going with the code produced by Xcode. Build and Run, and away I go...

Everything works great. I can add and delete records, edit them. I'm thinking, "Pretty Great! That wasn't too tough, and I've got a pretty slick, while simple, app going here!"

Well, when I quit without the required ISBN, I get an alert saying it is required. Good. Next I get an alert, "Could not save changes while quitting..." with a Quit Anyway button and a Cancel button. Again, this is straight from the produced code and expected behavior. Still Good.

But now is the problem. When I click Cancel, the app quits. (When I click Quit, it quits. I'm good with that.) One thing I notice is that if I leave the ISBN completely empty, I get this behavior. If I have an entry that does not meet the RegEx requirement, it gives me an option to Discard Changes, but it never gives me the Quit Anyway alert. Anyway, this line in makes me believe it should not quit when I click cancel.
Code:
// answer is an NSInteger returned from [NSAlert runModal]
if (answer == NSAlertAlternateReturn) return NSTerminateCancel;
That returns false (or NO, I guess, in Objective-C). I see it happen in the debugger. Also, when I look in the debugger, it shows that answer is set to 1001. (Strangely, from what I can see NSTerminateCancel should be 0. Not sure where 1001 comes from, but that's not real important to me right now. Should it be?) I can change the if to say if (answer == 1001) and it works as I want it. But, I can't understand why it doesn't recognize NSAlertAlternateReturn as the Cancel click.

Also, in the template code produced, there are the following comments:
Code:
// Typically, this process should be altered to include application-specific 
// recovery steps.
I guess I'm at a loss as to what the correct "recovery steps" might be.

Am I getting ahead of myself here? Am I digging too deep while still at an admittedly novice level? Maybe so, but it doesn't seem like this should be this confusing/difficult. I keep seeing things stating that when learning Cocoa you should let the framework do its job and not try to micro-manage things--I'm trying to keep that in mind.

Sorry for the long post, and I hope it's not too confusing. I tried to explain my situation as best I can. Thanks for any and all help.
Thom_Edwards is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
iPhone 5 black screen. Power button and home button not working. brian4la iPhone Tips, Help and Troubleshooting 2 Jun 25, 2014 06:54 PM
iOS 7 beta 6 - Phone not waking on first attempt (Power button or Home button) DDustiNN iOS 7 9 Sep 30, 2013 08:03 PM
I want to cancel my ATT Iphone5 -You can have it. anyone figure out HOW to cancel? nostresshere iPhone 3 Sep 26, 2012 04:53 PM
Cancel Online Pre-Order or Let Apple Employee at Apple Store Cancel tomorrow? amitdoc2b iPhone 5 Sep 21, 2012 12:55 PM
iPhone 4 Sleep/wake button, vibrate button, and volume button not responsive McEggrolls iPhone 6 Sep 20, 2012 10:16 AM

Forum Jump

All times are GMT -5. The time now is 01:32 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC