【问题标题】:Using NSAlert beginSheetModalForWindow with contextInfo将 NSAlert beginSheetModalForWindow 与 contextInfo 一起使用
【发布时间】:2011-07-28 07:43:20
【问题描述】:

当用户点击默认按钮时,我从下面的警报表代码中收到以下错误:

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

顺便说一句,代码在 Snow Leopard 中运行良好,但在 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"]]]; 
    }       
}

【问题讨论】:

  • 您的代码对我来说看起来不错。我看不出错误的根源是什么。它一定在别的地方。
  • 顺便说一句,代码在 Snow Leopard 上运行良好,但在 Lion 中出现了问题。
  • 如果您有 Xcode 4.1,请查看有关您正在使用的所有方法的文档。其中之一可能已经改变。抱歉,没有更好的计划。

标签: objective-c cocoa macos


【解决方案1】:

如果您使用垃圾回收,请将extrasDict 存储在实例变量中——任何地方都无所谓——因此它不会在运行循环结束时释放。您需要保留字典,直到 didEndSelector 可以对其采取行动。

【讨论】:

  • 感谢您的回答,但我不敢相信您必须这样做(虽然您是正确的,但您的回答救了我)。为什么 #beginSheetModalForWindow 在方法运行完成之前不保留 contextInfo?
  • 我认为这是因为contextInfo 绝对可以是任何东西——它不仅限于id
【解决方案2】:

我已经执行了你的编码部分。它运作良好。检查编码的剩余部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 2011-12-14
    • 2014-01-17
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多