我想向菜单栏可可桌面应用程序中添加一些数组元素

这是我的错误代码

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


我是可可的新手,我试图自己解决这个问题。

我发现

[__NSCFNumber length]


所以我认为问题出在这条线上

 NSStatusItem *statusItem = [[bar statusItemWithLength:NSVariableStatusItemLength]retain];


该行是从a​​pple的示例复制而来的:)它使状态项的长度动态变化,以适应其内容的宽度。 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时代码可以正常工作,并且lengthNSString上的常用方法。)

我需要查看您对getArray的实现,以确切地了解出了什么问题,但是我敢打赌,知道您偶然在其中拥有NSNumber足以解决您的问题。 (顺便说一下,将访问者的名称简称为array而不是getArray是可可风格的。)

关于macos - cocoa 菜单栏错误[__NSCFNumber长度]:无法识别的选择器已发送到实例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9427848/

10-11 00:32
查看更多