gpt4 book ai didi

iphone - 在 UIPopover 中停止 NSTimer

转载 作者:行者123 更新时间:2023-11-28 23:12:48 24 4
gpt4 key购买 nike

所以我有一个带有 NSTimer 的 UIPopover。它有各种 BOOLS 和属性来实现其他方法,但现在这并不重要。我想知道的是,当popover的parent view消失后,有什么方法可以释放并使timer失效吗?我尝试了使计时器无效并释放计时器的类方法,但它们给了我 SIGABRT 和 EXC_BAD_ACCESS 警告。我认为这与计时器在它已经为零时被告知 dealloc 有关。

几个额外的因素:1. 计时器必须保持有效并在弹出窗口消失时分配。2.当父 View 消失时,计时器必须失效并释放。

有什么建议吗?

代码:

//@interface
NSTimer *clickTimer;
@property (nonatomic, retain) NSTimer *clickTimer;

//.m
- (IBAction)clickerPressed:(id)sender
{
if ([click isClicking] == YES) {
[clickTimer invalidate];
[clickTimer release];
clickTimer = nil;
// enable the idle timer when the clicking is turned off
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
[click setIsClicking:NO];
[clickerButton setBackgroundImage:[UIImage imageNamed:@"wood.png"] forState:UIControlStateNormal];
[clickerButton setTitle:@"Start" forState:UIControlStateNormal];
[clickStatus setText:[NSString stringWithFormat:@"%d",[click numberOfBeatsToDisplay]]];
[click setClickCount:0];

}
else {
// disable the idle timer while the metronome is clicking.
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
[click setIsClicking:YES];
[clickerButton setBackgroundImage:[UIImage imageNamed:@"wood.png"] forState:UIControlStateNormal];
[clickerButton setTitle:@"Stop" forState:UIControlStateNormal];
[[NSThread currentThread] setThreadPriority:1.0];

clickTimer = [[NSTimer scheduledTimerWithTimeInterval:[click clickRateInSeconds] target:self selector:@selector(click:) userInfo:nil repeats:YES]retain];
}

最佳答案

释放对象不会自动将 nil 分配给指针。在发布声明后添加 clickTimer = nil;。这将防止另一个方法在尝试释放 clickTimer 时访问无效对象。

为确保您的所有方法都将 clickTimer 正确设置为 nil,您可以考虑创建一个方法来为您使计时器无效,而不是让每个方法都使计时器:

- (void) invalidateClickTimer
{
// invalidate timer
if (clickTimer == nil)
return;
[clickTimer invalidate];
[clickTimer release];
clickTimer = nil;

// stop clicking
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
[click setIsClicking:NO];
[clickerButton setBackgroundImage:[UIImage imageNamed:@"wood.png"] forState:UIControlStateNormal];
[clickerButton setTitle:@"Start" forState:UIControlStateNormal];
[clickStatus setText:[NSString stringWithFormat:@"%d",[click numberOfBeatsToDisplay]]];
[click setClickCount:0];
return;
}

然后替换 - (IBAction)clickerPressed:(id)sender 中的以下行

if ([click isClicking] == YES) {
[clickTimer invalidate];
[clickTimer release];
clickTimer = nil;
// enable the idle timer when the clicking is turned off
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
[click setIsClicking:NO];
[clickerButton setBackgroundImage:[UIImage imageNamed:@"wood.png"] forState:UIControlStateNormal];
[clickerButton setTitle:@"Start" forState:UIControlStateNormal];
[clickStatus setText:[NSString stringWithFormat:@"%d",[click numberOfBeatsToDisplay]]];
[click setClickCount:0];
}

用这一行:

if ([click isClicking] == YES) {
[self invalidateClickTimer];
}

从父 View 的 UIViewController 调用新方法 - (void) invalidateClickTimer。可能在方法 - (void)viewWillDisappear:(BOOL)animated

关于iphone - 在 UIPopover 中停止 NSTimer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7692602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com