美文网首页
iOS判断控制器是push还是modal弹出

iOS判断控制器是push还是modal弹出

作者: 字节码 | 来源:发表于2016-12-24 11:27 被阅读250次

由于presentviewcontroller的方式弹出的viewcontroller不会被添加到self.navigationController.viewControllers数组中,而通过push方式显示的viewcontroller会存在该数组的最后,所以可以通过遍历navigationController的子控制器来判断push还是modal:

- (BOOL)isPresent {
    
    BOOL isPresent;
    
    NSArray *viewcontrollers = self.navigationController.viewControllers;
    
    if (viewcontrollers.count > 1) {
        if ([viewcontrollers objectAtIndex:viewcontrollers.count - 1] == self) {
            
            isPresent = NO; //push方式
        }
    }
    else{
        isPresent = YES;  // modal方式
    }
    
    return isPresent;
}

当需要自定义导航返回按钮时,可进行判断是pop还是dismiss

- (void)backCompletionHandle:(nullable void(^)())block {
    
    if ([self isPresent]) {
        [self dismissViewControllerAnimated:YES completion:^{
            if (block) {
                block();
            }
        }];
    }else {
        [self.navigationController popViewControllerAnimated:YES];

        if (block) {
            block();
        }
    }
    
}

相关文章

网友评论

      本文标题:iOS判断控制器是push还是modal弹出

      本文链接:https://www.haomeiwen.com/subject/ntmlvttx.html