UlfDunkel
UlfDunkel

Reputation: 169

How do I get the contextInfo in beginSheetModalForWindow:completionHandler:?

I want to transition all my old usages of -beginSheetModalForWindow:modalDelegate:didEndSelector:contextInfo: to the recommended -beginSheetModalForWindow:completionHandler:. How do I define contentInfo: and get it in the completionhandler?

Here is an example of how the old code looks like:

[alert beginSheetModalForWindow:window
                  modalDelegate:self
                 didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) 
                    contextInfo:(void *)CFBridgingRetain(fc)];

The endSelector method looks like this:

- (void)alertDidEnd:(NSAlert *)alert returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
    if (returnCode == NSAlertDefaultReturn)
    {
        FileController *fc = (__bridge FileController *)(contextInfo);
         [...]
    }
}       

}

I guess the new method should look somewhat like this:

[alert beginSheetModalForWindow:window completionHandler:^(NSModalResponse alertReturnCode)
{
    if (alertReturnCode == NSAlertFirstButtonReturn)
    {
        // evaluate contextInfo here ...
    }
}];

But I have no clue how to get the contextInfo into the completionhandler.

Any help is appreciated.

Upvotes: 0

Views: 270

Answers (1)

matt
matt

Reputation: 535889

There is no context info because the completion handler block can simply look right at the surrounding environment.

NSString* s = @"heyho";
[alert beginSheetModalForWindow:window completionHandler:^(NSModalResponse alertReturnCode) {
    if (alertReturnCode == NSAlertFirstButtonReturn)
    {
        // s is visible here
    }
 }];

In other words, we don't need to pass a context because we are in a context. If you have a FileController to pass down into the block, just let it pass down into the block.

Upvotes: 2

Related Questions