Need help with Xcode alert sheet

Discussion in 'Mac Programming' started by NexesDev, May 14, 2008.

  1. NexesDev macrumors newbie

    Joined:
    May 12, 2008
    #1
    When I run the program everything works and the alert sheet comes up, but when I click the "OK" button to delete the folder, it does nothing, just sits there. What am I doing wrong? Any suggestions would be a great help.

    Here is the alert sheet part of the implementation file...

    -(IBAction)deleteFolder:(NSButton *)sender
    {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *title = @"Are you sure you want to delete your created folder?";
    NSString *defaultButton = @"YES";
    NSString *alternateButton = @"NO";
    NSString *msg = @"your friendly reminder";

    NSBeginAlertSheet(title,
    defaultButton,
    alternateButton,
    nil,
    window,
    self,
    @selector(sheetDidEnd:returncCode:contextInfo:),
    nil,
    nil,
    msg);

    [pool release];
    }

    -(void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
    {

    if (returnCode == NSAlertDefaultReturn)
    {
    NSFileManager *nsf = [NSFileManager defaultManager];

    [nsf removeFileAtPath:mad:"/Users/joeb/Desktop/NewFolderMan/" handler:nil];
    }
    }
     
  2. Streamer macrumors member

    Joined:
    Apr 18, 2008
    #2
    I'm still heavily learning this stuff, but I had a real headache as I experimented with that at the weekend. For me the problem was passing the "window" in the call, xcode kept warning me it would probably get ignored.

    I tried "self window","window" etc but all no joy. I think in the end "self delegate" did it. I'm not near my MBP at the moment so I may have misquoted that, but it sounds like you and I are at the same place in learning Cocoa :)

    Once I sorted that, it still didn't play ball, but I found using breakpoints my SheetDidEnd was getting called, but I had screwed up the return code check!

    Hope the above may help a little!
     
  3. Krevnik macrumors 68040

    Krevnik

    Joined:
    Sep 8, 2003
    #3
    Please use the code BB tag when posting code, it makes it easier to read. :)

    Have you tried placing a breakpoint on the 'if' line in your didEnd method, and see if you hit it?
     
  4. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #4
    I'm hardly an expert, but I have run alert sheets like this successfully.
    It looks fine.

    Try getting rid of the autorelease pool stuff. You don't need it and there's a chance it is freeing stuff that NSBeginAlertSheet allocated too early.
     
  5. Krevnik macrumors 68040

    Krevnik

    Joined:
    Sep 8, 2003
    #5
    I would get rid of the autorelease pool as well, but the odds of it releasing objects 'too early' are pretty much zero.

    The autorelease pool is /meant/ to operate correctly like that. Objects only get added if they are autoreleased (standard releases don't interact with the pool), and they only get released from the pool if their refcount hits zero as the pool drains.

    An object being created in an autoreleased state is passed around by reference along the stack, and code should be placing a retain on it if they intend to keep a reference around. If not, the safe assumption is that it is safe to release.

    If he did manage to trip across a bug with NSBeginAlertSheet() he would see exceptions getting thrown in his output log.
     
  6. NexesDev thread starter macrumors newbie

    Joined:
    May 12, 2008
    #6
    Hey guys thanks for the help

    Thanks for the help guys, i tried the idea of getting rid of the NSAutoReleasePool and it didn't work. However i finally got the alert sheet to work but i had to change it to this. My question now is, the new code seems like its less "sophisticated", is it a problem doing it like this? I had to use the NSRunAlertPanel instead of the NSBeginAlertSheet.

    Code:
    -(IBAction)deleteFolder:(NSButton *)sender
    {
    	
    	NSString *title = @"Are you sure you want to delete your created folder?";
    	NSString *defaultButton = @"YES";
    	NSString *alternateButton = @"NO";
    	NSString *msg = @"your friendly reminder";
    	NSInteger x;
    	
    	x = NSRunAlertPanel (title,
    					 msg,
    					 defaultButton,
    					 alternateButton,
    					 nil);
    						 
    	if (x == NSAlertDefaultReturn)
    	   {
    	     NSFileManager *nsf;
    		 nsf = [NSFileManager defaultManager];
    		 
    		 [nsf removeFileAtPath:@"/Users/joeb/Desktop/NewFolderMan" handler:nil];
    		} 
    	
    
    }
    
     
  7. caveman_uk Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #7
    That's how I would do it - although I'd make it less verbose
    Code:
    -(IBAction)deleteFolder:(NSButton *)sender
    {
    
    	NSInteger x = NSRunAlertPanel (NSLocalizedString(@"Are you sure you want to delete your created folder?",nil),
    				       NSLocalizedString(@"your friendly reminder", nil),
    				       NSLocalizedString(@"Yes",nil),
    				       NSLocalizedString(@"No",nil),
    				       nil);
    						 
    	if (x == NSAlertDefaultReturn)
    	{
    	     NSFileManager *nsf= [NSFileManager defaultManager];
    		 
    	     [nsf removeFileAtPath:@"/Users/joeb/Desktop/NewFolderMan" handler:nil];
            } 
    	
    
    }
    
     
  8. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #8
    So much for my NSAutoReleasePool idea!

    It's a moot point, but it bothers me that you had to just avoid NSBeginAlertSheet() altogether. I'm in the process of learning Cocoa, and I guess what bothers me is that I thought I understood NSBeginAlertSheet(). But I can't see what the problem was with your code. So I might *not* understand NSBeginAlertSheet() fully after all.

    Oh well, I'm glad you're up and going :)
     
  9. caveman_uk Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #9
    Unless it's a typo this could be the problem in the OP's code

    Code:
    @selector(sheetDidEnd:returncCode:contextInfo:),
    
    Note the extra 'c' in the returnCode. Maybe the method just never got called?
     
  10. NexesDev thread starter macrumors newbie

    Joined:
    May 12, 2008
    #10
    WOW thanks!

    Thank you caveman_uk, what you suggested works perfectly. now im just confused on why. When i look up the NSBeginAlertSheet in the Xcode documentation, it never says "returncCode" just "returnCode". Am I looking in the wrong place? Any way thanks you guys for your help!!!
     
  11. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #11
    Ah Ha! Nice catch caveman.

    sheetDidEnd:returncCode:contextInfo:

    and

    sheetDidEnd:returnCode:contextInfo:

    are two different methods

    That explains it: NexesDev, you told NSRunAlertPanel to send a message called "sheetDidEnd:returncCode:contextInfo:" to self. But it doesn't have a method called that. It has a method with a similar name; close but no cigar.

    It's worth noting that you could have called the method anything, as long as you are consistent (and specify the correct parameters). You could specify
    Code:
    @selector(pepperoni:pizza:pie:)
    in the call to NSRunAlertPanel() as long as you had a method
    Code:
    -(void)pepperoni:(NSWindow *)sheet pizza:(int)returnCode pie:(void *)contextInfo { ... }
     

Share This Page