objective-cxcodeuser-interfacenscontrol

How can I obtain window's NSControl* from custom sheet button?


I have a simple 'test bed' module consisting of an empty NSWindow. The only item on the window is a button labeled 'Toggle Window' and nothing else. It has one method shown below:

-(IBAction)testResize:(NSControl*)sender;
{
    NSRect frame = sender.window.frame;
    if(frame.size.width == 225)
        frame.size.width += 160;
    else
        frame.size.width -= 160;

    [sender.window.setFrame:frame display:YES animate:YES];
}

Clicking this button toggles the window's width between 225 and 385 pixels and works well. The plan is to add a similar method to my main app, but this time called from a button on a custom sheet, like so:

-(void)customSheetDidEnd:(NSAlert*)alert returnCode:(NSInteger returnCode contextInfo:(void*)contextInfo
{
    switch (returnCode)
        {    
        case 1000:
            {
            [self testResize:(NSControl*)theWindow];
            // print it ...
            break;
            }

            case 1001:
            {
            // save it...
            break;
            }

            case 1002:
            {
            // forget it...
            break;
            }
        }
}

This attempt does nothing, even after changing the called method's name to that shown above. I can't connect an IBAction in this case because the custom sheet buttons don't show in the .xib file of course. Is it (a) possible to pass this NSControl pointer to my method by modifying the call, or (b) access it from within the 'testResize' method itself (which would seem like the simplest solution)?

I’ve experimented with the above and searched on the net for days, but just can’t seem to find anything that seems to answer my specific question. Thanks.


Solution

  • Problem solved! While I'm fairly okay with the basics, some of the trickier stuff can throw me. After several more days of struggling I realised that passing the NSControl* was just an indirect way of getting at the main window rect (which I'd never encountered before) so in desperation I reverted to the more common way of accessing it. Simplified the method call in customSheetDidEnd to read:

    -(void)customSheetDidEnd:(NSAlert*)alert returnCode:(NSInteger returnCode contextInfo:(void*)contextInfo
    {
        switch (returnCode)
            {    
            case 1000:
                {
                [self testResize];  //### changed ###
                // print it ...
                break;
                }
    
                case 1001:
                {
                // save it...
                break;
                }
    
                case 1002:
                {
                // forget it...
                break;
                }
            }
    }
    

    and the receiving method to:

    -(void)testResize  //### changed ###
    {
        NSRect frame = [theWindow frame];
        if(frame.size.width == 225)
            frame.size.width += 160;
        else
            frame.size.width -= 160;
    
        [theWindow setFrame]:frame display:YES animate:YES];
    }
    

    Now working fine. Thanks to all.