Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 您将如何制作状态项';s的标题是图像而不是文本?_Objective C_Cocoa - Fatal编程技术网

Objective c 您将如何制作状态项';s的标题是图像而不是文本?

Objective c 您将如何制作状态项';s的标题是图像而不是文本?,objective-c,cocoa,Objective C,Cocoa,我已经为我的应用程序创建了一个状态项(菜单栏项),但现在它的标题是文本,我如何让它显示图像 [item setTitle:@"MenuItem"]; 这是设置标题的代码部分。我需要对代码做什么更改才能使其显示图像 [item setTitle:@"MenuItem"]; 使用该方法,为其提供要在菜单栏上显示的NSImage 还请注意,您可以使用一个显示图像和文本的NSStatusItem,例如使用电池指示灯 下面是一个将图像设置为名为OtherImage.jpg的文件的小示例: NSImag

我已经为我的应用程序创建了一个状态项(菜单栏项),但现在它的标题是文本,我如何让它显示图像

[item setTitle:@"MenuItem"];
这是设置标题的代码部分。我需要对代码做什么更改才能使其显示图像

[item setTitle:@"MenuItem"];
使用该方法,为其提供要在菜单栏上显示的
NSImage

还请注意,您可以使用一个显示图像和文本的
NSStatusItem
,例如使用电池指示灯

下面是一个将图像设置为名为
OtherImage.jpg的文件的小示例:

NSImage *statusItemImage = [NSImage imageNamed:@"OtherImage"];
if( !statusItemImage ) {
  NSLog(@"ERROR: Could not load the image for 'OtherImage.png'");
} else {
  [item setImage:statusItemImage];
}
确保文件OtherImage.png已实际添加到Xcode项目中,并复制到应用程序的
Resources
目录中。如果您的映像未加载,则意味着文件命名不正确,不是真正的映像,或者实际上不在应用程序包中

使用该方法,为其提供要在菜单栏上显示的
NSImage

还请注意,您可以使用一个显示图像和文本的
NSStatusItem
,例如使用电池指示灯

下面是一个将图像设置为名为
OtherImage.jpg的文件的小示例:

NSImage *statusItemImage = [NSImage imageNamed:@"OtherImage"];
if( !statusItemImage ) {
  NSLog(@"ERROR: Could not load the image for 'OtherImage.png'");
} else {
  [item setImage:statusItemImage];
}

确保文件OtherImage.png已实际添加到Xcode项目中,并复制到应用程序的
Resources
目录中。如果您的映像未加载,则意味着文件命名不正确,不是真正的映像,或者实际上不在应用程序包中

啊,我明白了,所以我会用setImage:(NSImage*)替换setTitle:@“MenuItem”,但是我在哪里输入我的图像的位置呢?首先加载图像。您可以使用类似NSImage*statImage=[nsimageimagenamed:@“MyStatusImage”];只需确保MyStatusImage.png或其他任何文件实际上位于应用程序的资源文件夹中。我将代码NSImage*statImage=[NSImage imageNamed:@“MyStatusImage”]?这个代码setImage:[[NSBundle mainBundle]pathForResource:@“OtherImage”的类型:@“png”]]@约书亚-在物品上使用setAlternateImage:。请看一下文档。Perspx为NSStatusItem类提供了一个指向文档的链接。啊,我明白了,所以我将用setImage:@“MenuItem”替换为setImage:(NSImage*),但是我在哪里输入图像的位置?您首先加载图像。您可以使用类似NSImage*statImage=[nsimageimagenamed:@“MyStatusImage”];只需确保MyStatusImage.png或其他任何文件实际上位于应用程序的资源文件夹中。我将代码NSImage*statImage=[NSImage imageNamed:@“MyStatusImage”]?这个代码setImage:[[NSBundle mainBundle]pathForResource:@“OtherImage”的类型:@“png”]]@约书亚-在物品上使用setAlternateImage:。请看一下文档。Perspx提供了一个指向NSStatusItem类文档的链接。