我正在發佈一個產品的兩個不同版本(如light和pro版本)。如果我無法以動態方式更改佔位符NewApplication,我不知道如何使用相同的MainMenu.xib。我想使用包名稱而不是「NewApplication」。 我希望有一個官方的方式來做到這一點,而不是黑客攻擊。以動態的方式擺脫NewApplication?
謝謝,
我正在發佈一個產品的兩個不同版本(如light和pro版本)。如果我無法以動態方式更改佔位符NewApplication,我不知道如何使用相同的MainMenu.xib。我想使用包名稱而不是「NewApplication」。 我希望有一個官方的方式來做到這一點,而不是黑客攻擊。以動態的方式擺脫NewApplication?
謝謝,
我會做這樣的:
NSMenu *menuBar = [NSApp mainMenu];
// we know that the application is always at the very left
NSMenu *applicationMenu = [menuBar itemAtIndex:0];
// we know that the quit-menu item is always the last element
NSMenuItem *quitMenuItem = [applicationMenu itemAtIndex:
[applicationMenu numberOfItems] - 1];
quitMenuItem.title = [quitMenuItem.title
stringByReplacingOccurrencesOfString:@"NewApplication"
withString:@"SomeOtherName"];
您還可以在要更改的菜單項上設置標籤。
你很可能想使用多目標路線;使用一個包含多個構建目標的Xcode項目,一個用於常規應用程序,另一個用於您的Lite版本。請參閱here。 (是的,這是正式在Xcode的支持,我知道是誰用這個策略來發布他們的應用程序商店應用程序的兩個版本的幾個應用程序開發人員。)
從gs的答案啓發,這裏是我已經添加到我的代碼。我在AppDelegate的awakeFromNib
中打電話setupBundleNameInMenuBar
。
此代碼將替換全部發生的「NewApplication」與應用程序名稱。
- (void)setupBundleNameInMenuBar {
NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleNameKey];
if (appName == nil) appName = [[NSProcessInfo processInfo] processName];
NSMenu *menuBar = [NSApp mainMenu];
for (NSMenuItem *menuItem in [menuBar itemArray])
[self replaceTitlePlaceholderInMenuItem: menuItem withString: appName];
}
- (void)replaceTitlePlaceholderInMenuItem:(NSMenuItem *)root withString:(NSString *)appName {
root.title = [root.title stringByReplacingOccurrencesOfString: @"NewApplication"
withString: appName];
NSArray *submenuItems = [root.submenu itemArray];
for (NSMenuItem *menuItem in submenuItems)
[self replaceTitlePlaceholderInMenuItem: menuItem withString: appName];
}
的Xcode 2.4.1
爲(NSMenuItem *菜單項在[菜單欄itemArray]) 錯誤:嵌套功能被禁止,則使用-fnested函數以重新啓用 錯誤:語法錯誤之前' '
root.title = [root.title stringByReplacingOccurrencesOfString:@「NewApplication」 withString:appName]; 錯誤:請求成員'標題'的東西不是結構或工會
Xcode版本問題?或者缺少一些頭文件?
[menuBar itemAtIndex:0]中的菜單項自動被$(PRODUCT_NAME)替換。所以沒有必要再次替換它。我希望找到一種方法來自動化子菜單項目,如「Quit NewApplication」,而不使用stringByReplacingOccurrencesOfString:withString::) – cocoafan 2009-09-24 22:00:36
請參閱我自己的回答 – cocoafan 2009-09-25 09:44:19
順便說一句,[menuBar itemAtIndex:0];將不會返回一個NSMenu。它將返回一個NSMenuItem。 – cocoafan 2009-09-27 17:55:46