fiz
October 8, 2012, 10:22am
#1
Hello,
I don’t understand how to pass a parameter in the contextInfo of a sheet alert.
An alert is deployed, which may be
“terminating”, and then I want it to close the document after OK is clicked
“not terminating”, and let it opened after dismissing with OK.
So I call (for example): [self userAlert: @“fatalErr” terminating: YES];
This is the code:
(void) userAlert: (NSString*) withKey isTerminating: (bool)terminating {
NSAlert *theAlert = [NSAlert alertWithMessageText:[[player selection]valueForKey:withKey]
defaultButton:nil alternateButton:nil otherButton:nil
informativeTextWithFormat:@“”];
[theAlert beginSheetModalForWindow:mainWindow
modalDelegate:self
didEndSelector:@selector (alertDidEnd:returnCode:contextInfo:)
contextInfo:&terminating];
NSLog(@“Passed %x”, terminating); // gives the correct value
}
(void)alertDidEnd:(NSAlert )alert returnCode:(NSInteger)returnCode contextInfo:(bool )contextInfo {
NSLog(@“Info %x”, *contextInfo); // gives zero.?
if (*contextInfo)[self close];
}
I’m missing something elementary here. Thank for your help.
StefanK
October 8, 2012, 10:42am
#2
Hi,
two problems:
¢ contextInfo is supposed to be a void pointer to an object
the contextInfo object isn’t retained automatically.
A workaround is to pass a string constant, which isn’t affected by the retain counter.
btw: the correct boolean type in ObjC is the capitalized BOOL
Try this
- (void) userAlert: (NSString*) withKey isTerminating: (BOOL )terminating {
NSAlert *theAlert = [NSAlert alertWithMessageText:[[player selection]valueForKey:withKey]
defaultButton:nil alternateButton:nil otherButton:nil
informativeTextWithFormat:@""];
[theAlert beginSheetModalForWindow:mainWindow
modalDelegate:self
didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
contextInfo:(terminating) ? @"YES" : @"NO"];
NSLog(@"Passed %x", terminating); // gives the correct value
}
- (void)alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
NSString *context = (NSString *)contextInfo;
NSLog(@"Info %@", context); // gives zero.?
if ([context isEqualToString:@"YES"]) [self close];
}
fiz
October 8, 2012, 11:19am
#3
Thanks Stefan. It works. Don’t forget the
NSString *context = ([b]__bridge[/b] NSString *)contextInfo;
if you’re using ARC (as I do).
McUsr
October 10, 2012, 9:05pm
#5
StefanK:
I don’t.
I don’t use Arc either, as I guess it is better to manage memory manually for speed, and trust the build an analyse, to find any flaws in my code, which have worked well so far.
But it would still be interesting to know how to enable it. I’m still on XCode 3.2
FYI, the claim is that ARC is actually faster, because it removes many redundant retain/release/autorelease calls.
I’m pretty sure it requires Xcode 4 – it certainly requires the use of Apple’s LLVM compiler.
McUsr
October 11, 2012, 5:43am
#7
Ahh. Faster is always better! And as long as it isn’t going to run on an iPhone.
And then I can also remove the tests to avoid the redundant calls then, apart from avoiding the unavoidable.
I’ll give Arc a close look.
Maybe it is time to upgrade! LLVM sounds good as well. (Though I thought I had it. )
McUsr
October 15, 2012, 1:27pm
#8
Hello!
I am investigating Xcode and what it can do for me at the moment! I do have GCC 4.2 and I can set “Use garbage collection” from the build settings when I scroll the list in build settings.