- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想向菜单栏 cocoa 桌面应用程序添加一些数组元素
这是我的代码,有错误
NSMenu *menu =[[NSMenu alloc]initWithTitle:@"menu"];
NSMenuItem *itemTest = [[NSMenuItem alloc] initWithTitle:@"Test" action:@selector(menuActionTest:) keyEquivalent:@"t"];
NSMenuItem *itemQuit = [[NSMenuItem alloc] initWithTitle:@"Quit App" action:@selector(menuActionQuit:) keyEquivalent:@"q"];
// add to menu
[menu addItem:itemTest];
[menu addItem:[NSMenuItem separatorItem]];
// handle the array, and here is the problem
NSArray* someList = [self getArray];
if ([someList count]>0) {
for(NSString *title in someList) {
NSLog(@"begin of add %@",title);
// here is the problem code
[menu addItem: [[NSMenuItem alloc] initWithTitle:title action:@selector(menuActionHelloWorld:) keyEquivalent:@""]];
NSLog(@"end of add %@",title);
}
[someList release];
[menu addItem:[NSMenuItem separatorItem]];
}
// add other to menu
[menu addItem:itemQuit];
//Define status bar-------------
NSStatusBar *bar = [NSStatusBar systemStatusBar];
NSStatusItem *statusItem = [[bar statusItemWithLength:NSVariableStatusItemLength]retain];
NSImage *menuImage = [NSImage imageNamed:@"status_off.png"];
[menuImage setTemplate:YES];
[statusItem setImage:menuImage];
[statusItem setHighlightMode:YES];
[statusItem setMenu:menu];
//Release----------------------
[itemTest release];
[itemQuit release];
[menu release];
当我使用时
[menu addItem: [[NSMenuItem alloc] initWithTitle:title action:@selector(menuActionHelloWorld:) keyEquivalent:@""]];
应用程序可以很好地构建,但是当我单击菜单栏中的状态图标时,收到错误消息
2012-02-24 16:20:38.393 MyApp[1546:503] -[__NSCFNumber length]: unrecognized selector sent to instance 0x202ecc3
2012-02-24 16:20:38.394 MyApp[1546:503] -[__NSCFNumber length]: unrecognized selector sent to instance 0x202ecc3
2012-02-24 16:20:38.398 MyApp[1546:503] (
0 CoreFoundation 0x00007fff85ebbfc6 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff86c1cd5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff85f482ae -[NSObject doesNotRecognizeSelector:] + 190
3 CoreFoundation 0x00007fff85ea8e73 ___forwarding___ + 371
4 CoreFoundation 0x00007fff85ea8c88 _CF_forwarding_prep_0 + 232
5 CoreFoundation 0x00007fff85e1f616 CFStringGetLength + 118
6 HIToolbox 0x00007fff8cb95d07 _Z12CheckForDashP8MenuDatathPKhPK10__CFString + 100
7 HIToolbox 0x00007fff8cb95c84 _Z26CleanupAfter1ItemInsertionP8MenuDatatPKhPK10__CFStringj + 41
8 HIToolbox 0x00007fff8cb95823 _Z31_InsertMenuItemTextWithCFStringP8MenuDataPK10__CFStringtjj + 204
9 AppKit 0x00007fff84ff8cfd -[NSCarbonMenuImpl _carbonMenuInsertItem:atCarbonIndex:] + 499
10 AppKit 0x00007fff8513c1a2 -[NSCarbonMenuImpl _privatePopulateCarbonMenu] + 298
11 AppKit 0x00007fff851c17a5 -[NSCarbonMenuImpl _populatePrivatelyIfNecessary] + 70
12 AppKit 0x00007fff851c174b -[NSCarbonMenuImpl _checkoutMenuRefWithToken:creating:populating:] + 298
13 AppKit 0x00007fff852edd46 -[NSCarbonMenuImpl _maximumSizeForScreen:] + 64
14 AppKit 0x00007fff8545f72b -[NSMenu size] + 35
15 AppKit 0x00007fff855517df +[NSStatusBarButtonCell popupStatusBarMenu:inRect:ofView:withEvent:] + 422
16 AppKit 0x00007fff85551b85 -[NSStatusBarButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 147
17 AppKit 0x00007fff850d2bde -[NSControl mouseDown:] + 786
18 AppKit 0x00007fff8509d6e0 -[NSWindow sendEvent:] + 6306
19 AppKit 0x00007fff85552598 -[NSStatusBarWindow sendEvent:] + 66
20 AppKit 0x00007fff8503616d -[NSApplication sendEvent:] + 5593
21 AppKit 0x00007fff84fcc1f2 -[NSApplication run] + 555
22 AppKit 0x00007fff8524ab88 NSApplicationMain + 867
23 RandomApp 0x00000001067390e2 main + 34
24 RandomApp 0x00000001067390b4 start + 52
我是Cocoa的新手,我尝试自己解决这个问题。
我发现了
[__NSCFNumber length]
所以我认为问题是从这一行出现的
NSStatusItem *statusItem = [[bar statusItemWithLength:NSVariableStatusItemLength]retain];
此行是从苹果示例复制的:)它使状态项长度动态化,调整其内容的宽度。 NSStatusBar Class Reference
我想我设置了一个可变长度的 NSString 作为标题,因此应用程序无法决定其长度值应该是多少。
为了证明这一点,我将代码编写为
[menu addItem: [[NSMenuItem alloc] initWithTitle:@"HelloWorld" action:@selector(menuActionHelloWorld:) keyEquivalent:@""]];
效果很好。
但我确实需要动态获取标题。
那么,我该如何修复我的代码?
谢谢!
编辑1:
谢谢
我添加
NSString *newTitle = [NSString stringWithFormat:@"%d",title];
对于我的代码,它有效。
所以我认为问题出在我的方法 getArray
这是我的 getArray 方法中的一些代码
NSMutableArray *List = [[NSMutableArray alloc] init];
for ( some conditions) {
// get something from a JSON result
NSString *title = [object valueForKeyPath:@"id"];
[List addObject:title];
}
return List;
在我看来,List 是一个由大量 NSString 元素组成的数组,但我认为我错了。我应该做什么来改进我的代码?好多了?
最佳答案
在我看来,您不小心传递了一个 NSNumber
作为您的标题。 (我猜测这是因为代码在添加 HelloWorlds 时工作正常,并且 length
是 NSString
上的常用方法。)
我需要查看您的 getArray
实现才能确切地知道出了什么问题,但我敢打赌,如果您意外地知道其中有一个 NSNumber
,将会足以让你解决它。 (顺便说一句,将访问器简单命名为 array
,而不是 getArray
,这是 Cocoa 风格。)
关于macos - cocoa 菜单栏错误 [__NSCFNumber length] : unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9427848/
我是 Mac OS X 开发新手。在 XCode/Cocoa 开发环境中,每个 Objective-C 项目都以 开始 #import 它工作正常,但我对 Cocoa.h 文件位置感到困惑。我的文件
所以我开始阅读这本书: http://www.amazon.com/Cocoa-Design-Patterns-Erik-Buck/dp/0321535022 第 2 章解释了 MVC 设计模式,并给
我使用下面的代码来访问项目中的变量 appDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate]; UIApplic
我想从我的表格 View 中拖动一行并将其放入 Mac OS X 10.6 中的任何其他 NSTextField 中,并放置一串文本。 拖放已经在我的应用程序中工作(在 NSTableView 和 N
如何在另一个窗口中加载 Nib ? 我尝试了 initWithWindowName, if (mmController == NULL) mmController = [[mainMenu a
其中一个类使用#import 。所以,在我的Podspec ,我包括 framework Cocoa 我的Podspec是 Pod::Spec.new do |s| s.name
有没有可以让我创建简单的条形图和折线图的框架? 最佳答案 有更新的开源Core Plot。 关于cocoa - cocoa :创建图表,我们在Stack Overflow上找到一个类似的问题: htt
如何从 SIMBL 插件获取主应用程序中的单例?当我尝试调用诸如 [ProcessControl sharedInstance] 之类的内容时,我收到一条错误,指出 ProcessControl 未定
我正在尝试从我的 cocoa 应用程序(通过使用 Mail.app)发送电子邮件中的一些文本。最初我尝试使用 HTML 发送格式正确的文本。但 mailto: URL 不支持 html 标签(即使在设
我正在创建一个应用程序,该应用程序必须与服务器数据交互,然后相应地显示数据库中的结果。我正在用 Cocoa 编写客户端应用程序。 示例:用户登录到 Web 应用程序。他们有一些提交网络报告的选项。选项
我想创建一个可以在多个应用程序中使用的框架(如 coreData、CoreAudio 等)。 任何人都可以发布此链接或教程... 最佳答案 尝试苹果的 Framework Programming Gu
我正在使用 [[NSFontManager sharedFontManager] collectionNames] 获取所有集合,但我看到一些未翻译的字符串(例如“com.apple.AllFonts
我刚刚开始我的 cocoa 教育,我有一个简单的问题,我看到单击一个单词并使用 mac 文本转语音功能的能力表明文本是自动内置的。 (即 - 对于 hello world 应用程序,您可以单击 hel
我需要在加密中实现盐,但要做到这一点,我需要将其存储为我需要创建的文件格式,以便稍后检索它进行解密。在加密方面我是个菜鸟。文件格式规范如下: 密文:密文长度;salt:盐的长度; 然后将密文和盐写出来
有没有办法在Cocoa中以任意的能力创建贝塞尔路径?例如,对于我的应用程序的一部分,我需要一个起伏的单元格。因此,我想使用 10 到 50 个不同的点绘制一条曲线,形成一个循环。这些点将随机波动。我认
我想将声音存储在用户默认值中。既然我们不能直接存储语音,那么存储它的最佳方式是什么?安装新语音后,使用数组 [NSSpeechSynthesizer availableVoices] 中的索引可能会有
我一直在寻找解决方案,但不知道是否可以执行以下操作: 我有一个drawRect方法,我想要做的是将图形元素(例如矩形和线条)添加到当前 View 而不刷新它。我曾经调用 setNeedsDisplay
美好的一天! 我正在为 Mac OS X(不是 iPhone)开发提醒软件。它应该一天一次显示一个窗口。具体时间没有规定,只是一次。我怎样才能做到呢。它会自行注册以在登录后启动。我已经尝试过 NSTi
我只是想知道是否可以创建具有层次结构的下拉或弹出菜单?我目前正在开发的应用程序跟踪作业、类(class)和主题。当用户创建作业时,他们需要能够从下拉列表中选择它所属的类(class),但我也不希望通过
是否有任何 Cocoa Widget 可以用来构建典型的(除了 Interface Builder 中的)GUI 构建器属性检查器,例如 RealBasic 或 Delphi? 是否有一个网站列出了其
我是一名优秀的程序员,十分优秀!