NSMenuItem 中的 NSProgressIndicator 未在第二个显示器上更新 [英] NSProgressIndicator in NSMenuItem not updating on second display

查看:19
本文介绍了NSMenuItem 中的 NSProgressIndicator 未在第二个显示器上更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSMenu 附加到一个 NSStatusItem(一个菜单栏应用程序).下载文件时,我想在此菜单的项目中显示 NSProgressIndicator.我为这个进度指示器创建了一个 NSViewController 子类,具有以下属性:

I've got a NSMenu attached to a NSStatusItem (a menu bar application). While downloading a file I want to display a NSProgressIndicator in an item of this menu. I've created a NSViewController subclass for this progress indicator with the following properties:

@property NSUInteger current; // Bound to NSProgressIndicator value
@property NSString *status;   // Bound to a NSTextField value
@property NSUInteger total;   // Bound to NSProgressIndicator max value

需要时,我使用以下代码开始下载文件,该代码在 NSRunLoopCommonModes 中运行,以便在显示 NSMenu 时也调用委托方法(即在 NSEventTrackingRunLoopMode 中运行):

When needed I start the download of the file with the following code, which runs in NSRunLoopCommonModes so that the delegate methods are also called when the NSMenu is displayed (which runs in NSEventTrackingRunLoopMode):

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[connection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
[connection start];

didReceiveData 委托方法中,我然后在我的 NSViewController 子类上设置属性:

In the didReceiveData delegate method I then set the properties on my NSViewController subclass:

progressViewController.current = receivedData.length;
progressViewController.status = @"Downloading...";
progressViewController.total = expectedBytes;

如果我将属性绑定到标签,这会很好用,但是 NSProgressIndicator 仅在我之前打开菜单时更新,连接开始,并且不关闭直到下载完成.但是如果我之后打开它,进度指示器不会更新.它显示了我打开菜单时属性的值,但该值不会更新.进度指示器也没有动画.请注意标签工作...

This works great if I bind the properties to a label, but the NSProgressIndicator only updates if I open the menu before the connection is started, and don't close it again until the download is finished. But if I open it afterwards, the progress indicator doesn't update. It shows the value the properties had at the moment I opened the menu, but this value doesn't update. The progress indicator then isn't animated either. Note that labels do work...

我尝试使用 YES 手动调用 setNeedsDisplay 并在更改值时在自定义视图和进度指示器上调用 display om,但这不起作用.修复进度指示器的唯一方法是调用 [progress setHidden:YES];[progress setHidden:NO]; 快速连续,但这会导致进度指示器闪烁,当然是一个不好的解决方案.

I tried to manually call setNeedsDisplay with YES and call display om on both the custom view and the progress indicator when I change the values, but that doesn't work. The only thing that does fix the progress indicator is calling [progress setHidden:YES]; [progress setHidden:NO]; in rapid succession, but that causes the progress indicator to flicker, and is of course a bad solution.

我做错了什么?我该如何修复这个 NSProgressIndicator?

What am I doing wrong? How can I fix this NSProgressIndicator?

推荐答案

我遇到了同样的问题,这个答案解决了我的问题:https://stackoverflow.com/a/4692194/406677

I had the same problem and this answer solved my problem: https://stackoverflow.com/a/4692194/406677

迅速:

progressIndicator.performSelector("startAnimation:", withObject:self, afterDelay: 0.0, inModes: [NSEventTrackingRunLoopMode])

这篇关于NSMenuItem 中的 NSProgressIndicator 未在第二个显示器上更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆