1. 程式人生 > >ios 卡頓,push多次同一個頁面

ios 卡頓,push多次同一個頁面

場景:快速多次點選cell跳轉到另一個頁面,另一個頁面被push多次。

原因:push後的頁面有耗時操作或者剛好push到另一個頁面時,另一個頁面正好在reloadData卡住主執行緒。造成點選cell時卡住了。

解決方法:

重寫導航控制器的push方法。

#import "DemoNavViewController.h"


@interface DemoNavViewController () <UINavigationControllerDelegate>
// 記錄push標誌
@property (nonatomic, getter=isPushing) BOOL pushing;

@end


@implementation DemoNavViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
}


- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.pushing == YES) {
        NSLog(@"被攔截");
        return;
    } else {
        NSLog(@"push");
        self.pushing = YES;
    }
    
    [super pushViewController:viewController animated:animated];
}


#pragma mark - UINavigationControllerDelegate
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    self.pushing = NO;
}


@end