Coder Social home page Coder Social logo

cctbackbuttonaction's Introduction

Problem

You want to show an alert view when user taps the back button on a navigation bar, and prevent the navigating if user decides to stay on the top view controller.

There are quite some questions on this topic over at stackoverflow: link1, link2, link3.

This project tries to solve this problem with a reusable solution, which is based on two facts:

  • The method navigationBar:shouldPopItem: in UINavigationController will be called, while UINavigationController is not declared to conform the UINavigationBarDelegate.
  • The item passed to navigationBar:shouldPopItem: will vary, depending on how you pop the top view controller. If you navigate by tapping the back button, the item will be navigationItem of the "current" top controller. If you navigate by calling popViewControllerAnimated:, the item will be navigationItem of the "previous" controller.

This code has not been tested. There are not many lines of code, please use until you've reviewed it.

For more information, see this post.

Use CCTBackButtonActionHelper

  • Include CCTBackButtonActionHelper.h and CCTBackButtonActionHelper.m in your project.

  • Create a subclass of UINavigationController, import CCTBackButtonActionHelper.h, implement navigationBar:shouldPopItem: and create instances with this subclass:

    #import "CCTBackButtonActionHelper.h"

    - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
        BOOL should = [[CCTBackButtonActionHelper sharedInstance] navigationController:self
                                                                   navigationBar:navigationBar
                                                                   shouldPopItem:item];
        if (!should) {
            return NO;
        }

        return [super navigationBar:navigationBar shouldPopItem:item];
    }
  • In the view controller, implement cct_navigationBar:willPopItem: and show alert view there:
    - (void)cct_navigationBar:(UINavigationBar *)navigationBar willPopItem:(UINavigationItem *)item {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Are You Sure?"
                                                          message:nil
                                                         delegate:self
                                                cancelButtonTitle:@"No"
                                                otherButtonTitles:@"Yes", nil];
        [alertView show];
    }
  • In the same view controller, implement alert view delegate and pop view controller manually.
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        if (alertView.cancelButtonIndex == buttonIndex) {
            return;
        }

        [self.navigationController popViewControllerAnimated:YES];
    }

cctbackbuttonaction's People

Contributors

weipin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.