xcode 如何使用可可进度条?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9470113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:27:53  来源:igfitidea点击:

How to use cocoa progress bars?

objective-cxcodeprogress-bar

提问by CocoaCoder

I am a brand new Mac programmer and I need help on how to use NSProgressIndicator. I already looked for sample code, but couldn't find anything that helped.

我是一名全新的 Mac 程序员,我需要有关如何使用NSProgressIndicator. 我已经在寻找示例代码,但找不到任何有用的东西。

What I want to do is this:

我想做的是这样的:

-(IBAction)startProgressBar:(id)sender; {

    //I want to make the bar update itself by the  value of 1 until it is at the value of 100
    //Example: add 1 to bar every second until it is full

}

回答by Aaron

I think performSelector:withObject:afterDelaywill help you here.

我想performSelector:withObject:afterDelay会在这里帮助你。

Write a method that will increment your progress bar. At the end of that method call performSelector:withObject:afterDelayon the same method with a delay of 1 second until the bar is full.

编写一个方法来增加你的进度条。在该方法结束时调用performSelector:withObject:afterDelay相同的方法,延迟 1 秒,直到条形图已满。

You probably won't need to pass an object to that method, so you can just use nil.

您可能不需要将对象传递给该方法,因此您可以使用 nil。

EDIT

编辑

In your case I would recommend something like this:

在你的情况下,我会推荐这样的东西:

- (IBAction)startProgressBar:(id)sender
{
    // Initialize the progress bar to go from 0 to 100
    [progress setMinValue:0.0];
    [progress setMaxValue:100.0];
    [progress setDoubleValue:0.0];

    // Start the auto-increment calls
    [self incrementProgressBar];
}

- (void)incrementProgressBar
{
    // Increment the progress bar value by 1
    [progress incrementBy:1.0];

    // If the progress bar hasn't reached 100 yet, then wait a second and call again
    if([progress doubleValue] < 100.0)
        [self performSelector:@selector(incrementProgressBar) withObject:nil afterDelay:1];
}