Custom modal sheet

Discussion in 'Mac Programming' started by FrogNL, Jun 19, 2010.

  1. FrogNL macrumors newbie

    Joined:
    Jun 19, 2010
    #1
    I'm trying to show a custom modal sheet when the user clicks a button in a document based application. In that sheet the user can enter text and click two button: a cancel and ok button.

    I've read the documentation and I followed this tutorial. However, I can't get it to work.

    I've created a MyCustomSheet.xib file with a MyCustomSheet class that is a sub-class of the NSWindowController. In that class I only implement the closeMyCustomSheet method, from the tutorial. In the MyDocument class I've implemented the other functions. I've also created an instance variable myCustomSheet of type NSWindow.

    I get a couple of errors and don't know how to fix them. These are my errors:
    1. error: statically allocated instance of Objective-C class 'NSWindow'
    2. error: wrong type argument to unary exclamation mark
    3. error: incompatible type for argument 1 of 'beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo:'
    4. error: 'myCustomSheet' undeclared (first use in this function)

    Could you guys help me?
     
  2. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #2
    If you post the code, probably. The first error is just a missing "*" before the variable (pointer) name. The others we can only guess at without more info.
     
  3. FrogNL thread starter macrumors newbie

    Joined:
    Jun 19, 2010
    #3
    Thanks for the help! That first error was really stupid.. :

    I've got a MyCustomSheet.xib file and the file's owner is linked to the MyCustomSheet classes. These are the .h and .m files:
    Code:
    #import <Cocoa/Cocoa.h>
    
    @interface MyCustomSheet : NSWindowController {
    
    }
    
    - (IBAction)closeMyCustomSheet:(id)sender;
    
    @end
    
    
    
    #import "MyCustomSheet.h"
    
    @implementation MyCustomSheet
    
    - (IBAction)closeMyCustomSheet:(id)sender {
        [NSApp endSheet:myCustomSheet];
    }
    
    @end
    
    
    The methods in the MyDocument class that have something to do with the dialog:
    Code:
    - (void)showCustomSheet:(NSWindow *)window
    
    // User has asked to see the custom display. Display it.
    {
        if (!myCustomSheet)
    		//Check the myCustomSheet instance variable to make sure the custom sheet does not already exist.
            [NSBundle loadNibNamed: @"MyCustomSheet" owner: self];
    	
        [NSApp beginSheet: myCustomSheet
    	   modalForWindow: window
    		modalDelegate: self
    	   didEndSelector: @selector(didEndSheet:returnCode:contextInfo:)
    		  contextInfo: nil];
    	
        // Sheet is up here.
        // Return processing to the event loop
    }
    
    - (void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
        [sheet orderOut:self];
    }
    I hope you can use this to help me. Thanks on forehand! :)
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Errors 2 and 3 are because the type of the variable named 'myCustomSheet' is wrong.

    Error 4 is because you haven't declared a variable myCustomSheet. It could also be the cause of errors 2 & 3.

    You haven't shown any code that declares any variable named 'myCustomSheet'. If you have such code, post it. If you don't, then that's the cause of errors 2-4.
     
  5. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #5
    You should probably replace "!myCustomSheet" with "nil != myCustomSheet" - that will fix error 2.

    Note that myCustomSheet is apparently not defined anywhere - I assume this would be an instance of MyCustomSheet class, you probably want to make an instance variable in your document class for this. In addition, the beginSheet method wants a window for its first argument, not a window controller.

    Finally, you probably do not want to declare self as the owner of a nib you are manually loading. Define FilesOwner in the nib file as class MyCustomSheet, create an instance of that object, and make that the owner. Then you can use [myCustomSheet window] as the first argument to beginSheet.
     
  6. FrogNL thread starter macrumors newbie

    Joined:
    Jun 19, 2010
    #6
    Thanks a lot both of you! The second error is now solved thanks to Sydde's solution! However, I don't understand why the other code doesn't work. It's the code Apple provides and should work, isn't it?

    Chown, I've got an instance variable myCustomSheet. (code: NSWindow *myCustomSheet;)

    By replacing "!myCustomSheet" with "nil != myCustomSheet" the third error was also solved. I've now got two problems left:
    1. Error 3: That error occurs in the MyCustomSheet class, because I didn't declare the myCustomSheet instance variable there. I was able to remove the error by replacing it with self.window, but I don't think that will work.
    2. I have to pass the window when I call the showCustomSheet method. What should I put there? Obviously self.window isn't working, but what will?

    Sydde, I don't understand the rows I've quoted below, especially the last two. Could you please explain that further?

    Again: thanks! I really appreciate your help!
     
  7. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #7
    Try to contain objects and data within objects instead of spreading them all over the place. The object that is MyCustomSheet class should own the sheet and should handle what the sheet's objects do. In the nib file, set the class of FilesOwner to MyCustomSheet class (next to last tab in the inspector palette). Then connect that FilesOwner object to the window like you would an IBOutlet (the popup will show "window").

    Now make myCustomSheet an instance of MyCustomSheet instead of an NSWindow and change the code to

    myCustomSheet = [[MyCustomSheet alloc] init];
    [NSBundle loadNibNamed:mad:"MyCustomSheet" owner:myCustomSheet];

    Now you can get the window from myCustomSheet with [myCustomSheet window].

    I think it is risky practice to load a nib owned by self any time after initialization. In doing so, you are creating a rather fragile structure to your application that will be difficult to maintain. Try to keep everything contained in neat little boxes that are not woven into each other.
     
  8. FrogNL thread starter macrumors newbie

    Joined:
    Jun 19, 2010
    #8
    Thanks for your reply!

    I understand what you mean and I think it's a very good idea, however, it doesn't work. I get some extra errors.. :)

    I've declared myCustomSheet as a NSWindow, so I can't make it point to a class, isn't it? Also, isn't it true that "nil != myCustomSheet" is wrong? Shouldn't it be nil == myCustomSheet?

    Also, I still have the two errors I mentioned in my precious post. :) Do you know how to fix them?
     
  9. FrogNL thread starter macrumors newbie

    Joined:
    Jun 19, 2010
    #9
    I was able to fix the myCustomSheet class error fairly easy by just replacing myCustomSheet with myCustomSheetClass. I now only have the two errors from my previous posts left, so that is:
    1. Error 3: That error occurs in the MyCustomSheet class, because I didn't declare the myCustomSheet instance variable there. I was able to remove the error by replacing it with self.window, but I don't think that will work.
    2. I have to pass the window when I call the showCustomSheet method. What should I put there? Obviously self.window isn't working, but what will?
     
  10. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #10
    Maybe you could post the project as a zip archive. Exclude the build folder from the archive and it will be a very small file.
     
  11. FrogNL thread starter macrumors newbie

    Joined:
    Jun 19, 2010
  12. xCoder4 macrumors newbie

    Joined:
    Nov 17, 2010
    #12
    Hi,

    I don't know if you already have a solution to this, if so -- could you also send it to me ? I'm trying to fix the same problem(s)!

    Thanx.
     
  13. FrogNL thread starter macrumors newbie

    Joined:
    Jun 19, 2010
    #13
    Please enable private messaging, so I can send it to you. :)
     
  14. T'hain Esh Kelch macrumors 601

    T'hain Esh Kelch

    Joined:
    Aug 5, 2001
    Location:
    Denmark

Share This Page