我正在分发一种产品的两种不同版本(如光和专业版)。如果我不能以动态方式更改占位符NewApplication,我不知道如何使用相同的MainMenu.xib。我想使用包名称而不是“NewApplication”。 我希望有一种正式的方法可以在没有黑客攻击的情况下做到这一点。
谢谢,
答案 0 :(得分:4)
我会这样做:
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"];
您还可以在要更改的菜单项上设置标记。
答案 1 :(得分:3)
受到gs答案的启发,这是我添加到代码中的内容。我在AppDelegate的setupBundleNameInMenuBar
中呼叫awakeFromNib
。
此代码将使用应用程序名称替换所有出现的“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];
}
答案 2 :(得分:1)
您最有可能想要使用多目标路线;使用一个包含多个构建目标的Xcode项目,一个用于常规应用程序,另一个用于lite版本。见here。 (是的,这在Xcode中得到官方支持,我知道有几个应用程序开发人员使用这种策略在App Store上发布他们的应用程序的两个版本。)
答案 3 :(得分:1)
Xcode 2.4.1
for([menuBar itemArray]中的NSMenuItem * menuItem) 错误:禁用嵌套函数,使用-fnested-functions重新启用 错误:'in'之前的语法错误
root.title = [root.title stringByReplacingOccurrencesOfString:@“NewApplication” withString:appName]; 错误:在非结构或联合的情况下请求成员'title'
Xcode版本问题?或缺少一些头文件?