0

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.

Tom
  • 45
  • 7
  • The NSAlert just gives you the resultCode of the button pressed. Note that completion blocks are the current thing - selectors have been deprecated. – red_menace Jul 22 '19 at 15:25
  • @red_menace. Thanks for reply. I'm aware that an NSAlert just provides a result code but I thought to include NSControl* at this point -- providing I could find some way to access it and pass it along, since the missing pointer seems to be the only reason the code won't work. Another possibility I mentioned would be to access it from **within** my called 'testResize' method. I assume NSWindow has a struct that might contain this control pointer but I've been unable to find a detailed list of members in NSWindow's (assumed) struct layout. – Tom Jul 23 '19 at 03:24
  • 1
    I’m still not sure what “control” you are looking for. An IBAction is sent the object that calls it, but you don’t have any controls in the window that would use an action. If you are talking about the window itself, you already have that (from calling the sheet), so it can be used directly in the completion handler. – red_menace Jul 23 '19 at 11:41
  • @red_menace. Glad to hear that I'm not the only one 'confused' by the example code found on the net, which appeared to have been over-complicated by passing an NSControl pointer. Removing that solved the issue. Thanks for your input. – Tom Jul 25 '19 at 02:49

1 Answers1

0

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.

Tom
  • 45
  • 7