PopViewController strange behaviour

前端 未结 5 980
忘掉有多难
忘掉有多难 2021-01-20 03:01

Due to a weird request which I tried to turn down but it didn\'t work, I had to override the navigationBar\'s back Button.

I have made a custom UINavigationControlle

5条回答
  •  时光取名叫无心
    2021-01-20 03:54

    I'm not 100% certain but I don't think you should actually be popping the view controller in that delegate method.

    "should" delegate methods don't normally do something. They just assert whether something should or shouldn't be done.

    Change your method to this...

    - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
    
        if ([[self.viewControllers lastObject] isKindOfClass:[ViewController1 class]]) {
            ViewController1 *vc1 = (ViewController1 *)[self.viewControllers lastObject];
            [vc1 handleBackAction];
            if (vc1.canPopVC == YES) { 
                return YES;
            } else {
                return NO;
            }
        }
    
        return YES;
    }
    

    And see if it works.

    All I have done is removed the popViewController calls.

    EDIT - How to add a custom back button

    In a category on UIBarButtonItem...

    + (UIBarButtonItem *)customBackButtonWithTarget:(id)target action:(@SEL)action
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage:[UIImage imageNamed:@"Some image"] forState:UIControlStateNormal];
        [button setTitle:@"Some Title" forState:UIControlStateNormal];
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
        UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    
        return barButtonItem;
    }
    

    Now whenever you want to set a custom back button just use...

    UIBarButtonItem *backButton = [UIBarButtonItem customBackButtonWithTarget:self action:@selector(backButtonPressed)];
    

提交回复
热议问题