【问题标题】:NSTimer don't stop when I present another viewcontroller当我展示另一个视图控制器时,NSTimer 不会停止
【发布时间】:2019-10-17 08:48:17
【问题描述】:

我有一个 viewController1 设置定时器。
当计时器倒计时到 0 时,我会呈现 viewController2。
定时器也一直在运行,虽然我调用了定时器函数invalidate
然后我标记了当前的视图控制器代码,它似乎正确停止了。

//ViewController2 *vc = [[ViewController2 alloc] init];
//[self presentViewController:vc animated:false completion:nil];

代码有什么问题?

#import "ViewController.h"
#import <WebKit/WebKit.h>
#import "ViewController2.h"

@interface ViewController ()<WKScriptMessageHandler, WKNavigationDelegate,WKUIDelegate>

    @property (nonatomic,strong) WKWebView* webView;
    @property (nonatomic, strong) WKWebViewConfiguration * webConfig;
    @property (nonatomic, strong) NSTimer *timer;
    @property (nonatomic) int count;

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];

        self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:[self createWKWebApp]];
        [self.view addSubview: self.webView];
        [self.webView.configuration.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];
        self.webView.scrollView.bounces = NO;
        [self.webView setContentMode:UIViewContentModeScaleAspectFit];
        self.webView.navigationDelegate = self;
        self.webView.UIDelegate = self;

        NSURL *url = [NSURL URLWithString:@"https://www.google.com.tw"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [self.webView loadRequest:request];

        self.count = 5;

        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
    }

    -(void)timerFired {

        NSLog(@"===) self.count : %d", self.count);

            if (self.count == 0) {

                ViewController2 *vc = [[ViewController2 alloc] init];
                [self presentViewController:vc animated:false completion:^{
                    [self.timer invalidate];
                    self.timer = nil;
                }];

            } else {
                self.count -= 1;
            }

    }


    - (WKWebViewConfiguration *)createWKWebApp {
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
        WKUserContentController *userContent = [[WKUserContentController alloc] init];

        config.userContentController = userContent;

        return config;
    }

    - (void)userContentController:(WKUserContentController*)userContentController didReceiveScriptMessage:(WKScriptMessage*)message {


    }

    @end

【问题讨论】:

    标签: ios objective-c nstimer


    【解决方案1】:

    在呈现 ViewController 之前,停止计时器

    [self.timer invalidate];
     self.timer = nil;
    ViewController2 *vc = [[ViewController2 alloc] init];
    [self presentViewController:vc animated:false completion:nil];
    

    【讨论】:

    • “这是一个不间断的计时器”是什么意思?
    【解决方案2】:

    在 viewWillDisappear 委托方法中使计时器无效,并在视图再次出现时再次触发。

    【讨论】:

    • 点评来源: 您能提供一些代码来提出您的建议吗?否则这篇文章似乎没有为问题提供quality answer。请编辑您的答案并改进它,或者将其作为评论发布。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    相关资源
    最近更新 更多