使用NSAlert beginSheetModalForWindow和contextInfo [英] Using NSAlert beginSheetModalForWindow with contextInfo

查看:659
本文介绍了使用NSAlert beginSheetModalForWindow和contextInfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户点击默认按钮时,从警告表代码中得到以下错误:

I get the following error from the alert sheet code below when the user hits the default button:


- [NSRectSet objectForKey:] :无法识别的选择器发送到实例
0x400876300

-[NSRectSet objectForKey:]: unrecognized selector sent to instance 0x400876300

Btw,该代码在Snow Leopard中正常工作,但问题出现在Lion 。

Btw, the code works fine in Snow Leopard but the problem comes up in Lion.

NSMutableDictionary * extrasDict = [[NSMutableDictionary alloc] init];
[extrasDict setObject:@"http://www.google.com" forKey:@"link"];

NSAlert * alert = [NSAlert alertWithMessageText:@"Published Successfully"
                                  defaultButton:@"View Collage on Facebook"
                                alternateButton:nil
                                    otherButton:@"Cancel"
                      informativeTextWithFormat:@"Successfully published to Facebook."];

[alert beginSheetModalForWindow:myWindow
                  modalDelegate:self
                 didEndSelector:@selector(publishedSuccessfullyDidEnd:returnCode:contextInfo:)
                    contextInfo:extrasDict];             


- (void)publishedSuccessfullyDidEnd:(NSAlert *)alert
                         returnCode:(NSInteger)returnCode
                        contextInfo:(void *)contextInfo {
    if (returnCode == NSAlertDefaultReturn) {
        [[NSWorkspace sharedWorkspace] openURL:
         [NSURL URLWithString:[(NSDictionary*)contextInfo objectForKey:@"link"]]]; 
    }       
}


推荐答案

你使用垃圾回收,在一个实例变量中存储 extrasDict - 在任何地方,没有关系 - 所以它不会在运行循环结束时释放。你需要字典,直到 didEndSelector 可以操作。

If you're using garbage collection, store extrasDict in an instance variable – anywhere, doesn't matter – so it isn't released at the end of the run loop. You need the dictionary to stick around until the didEndSelector can act on it.

这篇关于使用NSAlert beginSheetModalForWindow和contextInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆