viewDisappear

时间:2015-07-03 10:50:13

标签: ios objective-c

我的目标是当viewDidLoad以频率0,8秒开始(刷新)并且在视图消失后停止(刷新)。

在viewDidLoad调用(refres)函数:

@proptery disAppear;
- (void)viewDidLoad {
   [super viewDidLoad];
   NSLog(@"TeklifVer-viewDidLoad()");
   self.disAppear = NO;
   [self refresh];    
}

- (void)viewDidDisappear:(BOOL)animated {
   NSLog(@"viewDiddisappear"); 
   self.disAppear = YES;
}

on(刷新)函数调用(刷新):[self performSelector:@selector(refresh) withObject:nil afterDelay:0.8];如果不是disAppear。

- (void) refresh
{
    NSLog(@"refresh");

    ...
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSDictionary * responseDictionary = (NSDictionary *)responseObject;

        if (!self.disAppear) {
            ...
            TeklifVerModel * teklifVerModel = [[TeklifVerModel alloc] initWithDictionary:responseDictionary];

            //init labels
            ...

            @try {
                if (teklifVerModel.ihaleStatus == 2) {
                    self.nextPage = true;
                } else if (teklifVerModel.ihaleStatus == 5) {
                    self.ihaleListesi = true;
                }
            }
            @catch (NSException *exception) {
                NSLog(@"error: %@", exception);
            }
        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error: %@", error);
    }];
    [operation start];

    if (self.nextPage) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        IhaleViewController * ihale = [storyboard instantiateViewControllerWithIdentifier:@"ihaleViewController"];
        [self presentViewController:ihale animated:YES completion:nil];
    } else if (self.ihaleListesi) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        IhaleListesiViewController * ihaleListesi = [storyboard instantiateViewControllerWithIdentifier:@"ihaleListesi"];
        [self presentViewController:ihaleListesi animated:YES completion:nil];
    } else if(!self.disAppear) {
        [self performSelector:@selector(refresh) withObject:nil afterDelay:0.8];
    }
}  

日志

2015-07-03 15:51:24.918 uztb[729:129628] refresh
2015-07-03 15:51:25.189 uztb[729:129628] viewDidDisappear
2015-07-03 15:51:25.720 uztb[729:129628] refresh
2015-07-03 15:51:26.522 uztb[729:129628] refresh
2015-07-03 15:51:27.324 uztb[729:129628] refresh
2015-07-03 15:51:28.127 uztb[729:129628] refresh

我的问题是当viewDisapper仍然执行某些(刷新)时。有时(刷新)永远不会停止。所以当这个视图再次加载多个(刷新)线程时

问题:viewDisappear停止(刷新)线程的最佳方法是什么?

1 个答案:

答案 0 :(得分:3)

尝试调用cancelPerformSelectorsWithTarget中的viewDidDisappear方法。 More info.