NSTimer не останавливаться, когда я представляю другой viewcontroller - PullRequest
0 голосов
/ 17 октября 2019

У меня есть viewController1, устанавливающий таймер.
Когда таймер обратного отсчета до 0, я представлю viewController2.
Таймер также продолжает работать, хотя я вызываю функцию таймера invalidate
Тогда япометить текущий код viewcontroller, кажется, он остановился правильно.

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

Ответы [ 2 ]

0 голосов
/ 17 октября 2019

Недействительно таймер в методе делегата viewWillDisappear и снова запускаем тигр всякий раз, когда представление снова появляется.

0 голосов
/ 17 октября 2019

Перед тем как представить ViewController, остановите таймер

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