Method Not Getting Called?

Discussion in 'Mac Programming' started by Darkroom, Jul 28, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    i have an NSBox that is used as a dropbox... whenever something is dropped into the drop box i want the performDragOperation method to direct to anotherMethod in my App Controller class, but it doesn't work... i'm pretty sure i have everything linked correctly in IB, so i don't understand what's going on.

    NSBox Class:
    Code:
    #import <Cocoa/Cocoa.h>
    #import "AppController.h"
    
    @interface DropBox : NSBox
    	{
    	IBOutlet AppController *appControllerRedirect;
    	}
    @end
    
    --------------------------------------------------
    
    #import "DropBox.h"
    #import "AppController.h"
    
    @implementation DropBox
    
    //some drag and drop methods
    
    - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
    	{
            //some pasteboard stuff
    	NSLog(@"item dropped in drop box"); [COLOR="SeaGreen"]//This gets called and displays In the console window, so i know the drag and drop methods within this class are coded correctly[/COLOR]
    	[appControllerRedirect alertSound:nil];  [COLOR="Red"]//This doesn't get called.  why not??!![/COLOR]
            return YES;
    	}
    
    AppController Class:
    Code:
    #import <Cocoa/Cocoa.h>
    
    @interface AppController : NSObject
            {
    
            }
    
    - (IBAction)alertSound:(id)sender;
    
    @end
    
    --------------------------------------------------
    
    #import "AppController.h"
    
    @implementation AppController
    
    - (IBAction)alertSound:(id)sender
    	{
    	NSBeep();
    	}
    
    i understand it is easy just to write NSBeep() in with the performDrapOperation method, but this isn't really my work – this is an example that illustrates my exact problem.

    any help would be appreciated.
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Are you sure appController is hooked up properly in IB?
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    i'm pretty sure... i right click on the Drop Box object in IB, and drag a line from the outlet "appControllerRedirect" to my AppController object... so shouldn't it work?
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Use the debugger. Set a breakpoint and step through the code.

    Or, just NSLog() the variable:
    Code:
    NSLog(@"appControllerRedirect: %@", appControllerRedirect);
     
  5. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #5
    would it matter if my drop box class is "Drop Box : NSBox" and not "Drop Box : NSObject"? i don't receive any warnings or errors, even after cleaning all... there must be something small i'm doing wrong here...
     
  6. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #6
    Here's A Small Example Project Of The Problem

    attached is my XCode project with this very problem. it's very small. only necessary files are included concerning this problem. there is a main window class that holds the NSBox class. the NSBox class is used as the file validator using Aquatic Prime validation. finally there is an app controller class with a single method that beeps if the file being dropped is not valid.

    also in the package is an invalid file (with specific extension) that can be used to test the drop box. everything is set up correctly (i think). once the user drops the invalid file into the drop box, aquatic prime will accept the file type, but will refuse the file, listing it as invalid in the console window. this is when it's also suppose to redirect to the method in the app controller class to NSBeep, but it does not get directed.

    if anyone can open this up in XCode, build it, and take a look at the test project and help me understand what's going wrong that would be great.
     

    Attached Files:

  7. lucasgladding macrumors 6502

    Joined:
    Feb 16, 2007
    Location:
    Waterloo, Ontario
    #7
    Found the problem...

    Your main window controller is creating the box instead of it being unarchived from the Nib file. You can fix things two ways:

    1:
    Drop an NSBox into the window in the nib and specify its class as RegistrationDropBox. You then need to move the drag type registration from initWithFrame to awakeFromNib in your RegistrationDropBox class. Note: awakeFromNib is called instead of initWithFrame when objects are unarchived rather than created programatically. Finally, remove the box creation code from your main window controller. I have attached your source with these modifications. It sounds the beep when you drop the file.

    2:
    Create accessor methods for the appController instance variable inside the RegistrationDropBox class and set them in the window controller when you create the box. When you create an object programatically, you also need to set the outlets and variables programatically. This is why your original source was not working. The object defined in your nib is different from the one created in the window controller.

    Looks good otherwise. Using an NSBox for drag and drop registration is a great idea.

    Best of luck
     

    Attached Files:

  8. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada

Share This Page