Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationController and back button action

I have an two controllers 1st is self and 2nd is maincontroller, where I'm pushing maincontroller in stack, so the back button is automatically coming.

Here I need to make an alert when the user presses the back button.

How can I do this?

like image 309
Harish Avatar asked Jan 10 '13 10:01

Harish


People also ask

How do I add back button to navigation bar storyboard?

Storyboard. You can also set this in the Storyboard. Select UINavigationBar and select the Attributes Inspector tab. Then you can change those two images under Back and Back Mask attributes.

How do I make a back button in Swift?

Back-button text is taken from parent view-controller's navigation item title. So whatever you set on previous view-controller's navigation item title, will be shown on current view controller's back button text. You can just put "" as navigation item title in parent view-controller's viewWillAppear method.

Which type of controller will automatically create a back button?

When you move between view controllers using UINavigationController , it automatically configures a Back button show either “Back” or the title of the previous view controller.


2 Answers

Or you can use the UINavigationController's delegate methods. The method willShowViewController is called when the back button of your VC is pressed.

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
like image 128
Satheesh Avatar answered Oct 11 '22 13:10

Satheesh


First hide the back button by using

self.navigationItem.hidesBackButton = YES;

and then create your own Custom Button:

UIBarButtonItem *backBtn =[[UIBarButtonItem alloc]initWithTitle:@"back" style:UIBarButtonItemStyleDone target:self action:@selector(popAlertAction:)];
self.navigationItem.leftBarButtonItem=backBtn;
[backBtn release];

and your selector is here:

- (void)popAlertAction:(UIBarButtonItem*)sender
{
    //Do ur stuff for pop up
}
like image 22
Satish Azad Avatar answered Oct 11 '22 14:10

Satish Azad