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

问题描述

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

  @property NSUInteger current; //绑定到NSProgressIndicator值
@property NSString * status; //绑定到NSTextField值
@property NSUInteger total; //绑定到NSProgressIndicator最大值

当需要时,我开始用以下代码下载文件, ( NSRunLoopCommonModes 中运行),以便在显示 NSMenu 时运行委托方法$ c> NSEventTrackingRunLoopMode ):

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

didReceiveData 委托方法中,我的 NSViewController 子类的属性:

  progressViewController.current = receivedData。长度; 
progressViewController.status = @下载...;
progressViewController.total = expectedBytes;

如果我将属性绑定到一个标签,但是 NSProgressIndicator 只有在我开始连接之前菜单时才会更新,并且在下载完成之前不要再次关闭。但如果我之后打开它,进度指示器不更新。它显示了我打开菜单时的属性值,但此值不更新。进度指示器也不是动画。请注意,标签工作...



我试图手动调用 setNeedsDisplay YES 并调用显示 om在自定义视图和进度指示器, t工作。修复进度指示器的唯一方法是调用 [progress setHidden:YES]; [progress setHidden:NO]; 快速连续,但会导致进度指示器闪烁,这当然是一个坏的解决方案。



我做错了什么?如何解决这个 NSProgressIndicator

解决方案

而这个答案解决了我的问题:

在swift中:

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


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

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];

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

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

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...

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.

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

解决方案

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

In swift:

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

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

09-02 05:51