从函数中获取局部变量并在另一个可以动态更改的函数中实现该变量

发布于 2024-11-03 16:03:09 字数 746 浏览 0 评论 0原文

我的应用程序有一个函数,它从 NSTextField 获取一个值,然后声明该变量,如下所示:

- (IBAction)startTimer

//all the other code

int totalTime = secs + hoursInSeconds + minutesInSeconds

timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerHandler) userInfo:nil repeats:YES];

然后,我想在另一个函数中使用局部变量 totalTime处理NSTimer

- (void)timerHandler

//all other code

totalTime =  totalTime - 1;   

//invalidate timer when it reaches 0
if (totalTime == 0.0) {
    [timer invalidate];

然而,由于变量totalTime是一个局部变量,我无法使用该值,并且我无法移动代码,因为NSTimer每1秒调用一次它,并且用户可能会更改该变量(从而重新声明它)。

那么,有什么方法可以从函数中获取局部变量并在另一个可以动态更改的函数中实现该变量吗?或者我可以仅使用一个函数来实现 NSTimer 倒计时吗

my app has a function, it gets a value from a NSTextField and then declare the variable, like this:

- (IBAction)startTimer

//all the other code

int totalTime = secs + hoursInSeconds + minutesInSeconds

timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerHandler) userInfo:nil repeats:YES];

then, i want to use the local variable totalTime in another function which processes the NSTimer.

- (void)timerHandler

//all other code

totalTime =  totalTime - 1;   

//invalidate timer when it reaches 0
if (totalTime == 0.0) {
    [timer invalidate];

however, as the variable totalTime is a local variable, i cannot use the value, and i cannot move the code over as NSTimer calls it every 1 sec and as the user may change the variable (and thus redeclaring it).

so, is there any way i can get a local variable from a function and implement the variable in another function which can be changed dynamically? or can i implement a NSTimer countdown by just using one function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漫雪独思 2024-11-10 16:03:09

您可以将该值包装在计时器的 userInfo 中:

NSNumber *totalTimeNumber = [NSNumber numberWithInt:totalTime];
timer = [NSTimer scheduledTimerWithTimeInterval:... target:... selector:... userInfo:totalTimeNumber repeats:...];

或者只是将其设为实例变量。

You could wrap the value in the timer's userInfo:

NSNumber *totalTimeNumber = [NSNumber numberWithInt:totalTime];
timer = [NSTimer scheduledTimerWithTimeInterval:... target:... selector:... userInfo:totalTimeNumber repeats:...];

Or just make it an instance variable.

你怎么敢 2024-11-10 16:03:09

好吧,这是一个有趣的变量,它可以使用局部变量,而不是实例变量,但仅适用于 Mac OS 10.6/iOS 4 及更高版本:

-(IBAction)startTimer:(id)sender
{
    // ensure, that the variables we'll capture in the block are mutable
    __block int totalTime = ...
    __block NSTimer *timer;
    void (^timerBlock)() = ^{
        if (--totalTime <= 0) { // this comparison is much less fragile...
            [timer invalidate];
        }
    };
    // If you'd call timerBlock() at this point you'll crash because timer contains junk!
    // However, (since timer is declared as __block) we can give it a meaningful value now and have it updated inside of the block, as well:
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerHandler:) userInfo:timerBlock repeats:YES];
}

-(void)timerHandler:(NSTimer*)timer
{
   ((void (^)())[timer userInfo])(); // retrieve the block and run it
}

警告:
由于我是从手机发送此消息,因此我不能 100% 确定 timerHandler: 中的转换。但这是沿着这条线的事情......
您应该能够完全省略演员阵容,但肯定会看到警告。

Well, here's a fun one that works with local variables, instead of instance variables but only on Mac OS 10.6/iOS 4 and above:

-(IBAction)startTimer:(id)sender
{
    // ensure, that the variables we'll capture in the block are mutable
    __block int totalTime = ...
    __block NSTimer *timer;
    void (^timerBlock)() = ^{
        if (--totalTime <= 0) { // this comparison is much less fragile...
            [timer invalidate];
        }
    };
    // If you'd call timerBlock() at this point you'll crash because timer contains junk!
    // However, (since timer is declared as __block) we can give it a meaningful value now and have it updated inside of the block, as well:
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerHandler:) userInfo:timerBlock repeats:YES];
}

-(void)timerHandler:(NSTimer*)timer
{
   ((void (^)())[timer userInfo])(); // retrieve the block and run it
}

Caveat:
Since I'm sending this from my phone, I am not 100% sure about the cast in timerHandler:. But it's something along this line...
You should be able to omit the cast altogether, but will definitely see a warning then.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文